Java面向对象的三大特性之一是继承,继承是指子类继承父类的属性和方法,这是代码重用的核心。继承用法的实例分析可以通过以下步骤进行,包括创建父类和子类,使用super关键字引用父类的方法和属性,对子类的属性和方法进行修改和继承等。
步骤一:创建父类和子类
创建父类,可以使用关键字class,后跟类名和一对大括号{},父类中需要定义一些属性和方法。例如:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak() {
System.out.println("I am a person");
}
}
创建子类,使用class关键字后跟子类名和extends关键字后跟父类名,然后定义子类的属性和方法。例如:
public class Student extends Person {
private int grade;
public Student(String name, int age, int grade) {
super(name, age);
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public void speak() {
System.out.println("I am a student");
}
}
步骤二:使用super关键字引用父类的方法和属性
子类可以使用super关键字调用父类的方法和属性,例如:
public class Student extends Person {
//...
public void speak() {
super.speak();
System.out.println("I am a student");
}
}
在这个例子中,子类的speak方法通过super.speak()调用了父类的speak方法,并在父类的speak方法后面增加了一些内容。
步骤三:对子类的属性和方法进行修改和继承
子类可以修改父类的属性和方法,也可以继承父类的属性和方法。例如:
Student student = new Student("Tom", 18, 3);
student.setName("Jerry");
System.out.println(student.getName());
student.speak();
在这个例子中,子类Student通过继承了父类Person的getName和speak方法,同时重写了speak方法,当我们调用子类的speak方法时,控制台将输出“I am a student”。另外,子类Student还新增了grade属性和setGrade、getGrade方法。
继承的好处在于子类可以重用父类的代码,减少了代码的重复,提高了代码的可维护性,在实践中发挥了重要的作用。
示例2:
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat() {
System.out.println("I am eating!");
}
}
public class Cat extends Animal {
private String color;
public Cat(String name, int age, String color) {
super(name, age);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void eat() {
System.out.println("I am a cat and I like to eat fish!");
}
}
public class Dog extends Animal {
private String food;
public Dog(String name, int age, String food) {
super(name, age);
this.food = food;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public void eat() {
System.out.println("I am a dog and I like to eat bones!");
}
}
public class Test {
public static void main(String[] args) {
Animal animal1 = new Animal("Animal1", 3);
animal1.eat();
Cat cat = new Cat("Cat1", 1, "black");
System.out.println(cat.getName() + "-" + cat.getAge() + "-" + cat.getColor());
cat.eat();
Dog dog = new Dog("Dog1", 2, "bones");
System.out.println(dog.getName() + "-" + dog.getAge() + "-" + dog.getFood());
dog.eat();
}
}
在这个例子中,Animal类为父类,Cat类和Dog类都继承了Animal类。子类Cat和Dog分别新增了color属性和food属性,并分别重写了eat方法。运行程序后控制台输出:
I am eating!
Cat1-1-black
I am a cat and I like to eat fish!
Dog1-2-bones
I am a dog and I like to eat bones!
从输出结果看,父类的eat方法被子类重写,而父类的属性被子类继承和修改。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java面向对象的三大特性之一继承用法实例分析 - Python技术站