class Person {
private String name;
private int age;
public String getName() {...}
public void setName(String name) {...}
public int getAge() {...}
public void setAge(int age) {...}
}
class Student extends Person {
// 不要重复name和age字段/方法,
// 只需要定义新增score字段/方法:
private int score;
public int getScore() { … }
public void setScore(int score) { … }
}
Student(子类),Person(父类)
extends
关键字来实现继承,可以不用再Student
中写Person
相同的部分。private
字段或者private
方法,但是可以把private
改为protected
,用protected
修饰的字段可以被子类访问。class Student extends Person {
public String hello() {
return "Hello, " + super.name;
}
}
super.fieldName
,this.fieldName
和fieldName
的形式来引用。super
,如果父类没有默认的构造方法,子类就必须显式调用super()
Student
,那么它可以指向一个Student
类型的实例:Student s = new Student();
Person
,那么它可以指向一个Person
类型的实例:Person p = new Person();
Student s = new Student();
Person p = s; // upcasting, ok
Object o1 = p; // upcasting, ok
Object o2 = s; // upcasting, ok
继承树是Student > Person > Object
,所以,可以把Student
类型转型为Person
,或者更高层次的Object
。
Person p1 = new Student(); // upcasting, ok
Person p2 = new Person();
Student s1 = (Student) p1; // ok
Student s2 = (Student) p2; // runtime error! ClassCastException!
多态是指,针对某个类型的方法调用,其真正执行的方法取决于运行时期实际类型的方法。
public class Main {
public static void main(String[] args) {
// 给一个有普通收入、工资收入和享受国务院特殊津贴的小伙伴算税:
Income[] incomes = new Income[] {
new Income(3000),
new Salary(7500),
new StateCouncilSpecialAllowance(15000)
};
System.out.println(totalTax(incomes));
}
public static double totalTax(Income... incomes) {
double total = 0;
for (Income income: incomes) {
total = total + income.getTax();
}
return total;
}
}
class Income {
protected double income;
public Income(double income) {
this.income = income;
}
public double getTax() {
return income * 0.1; // 税率10%
}
}
class Salary extends Income {
public Salary(double income) {
super(income);
}
@Override
public double getTax() {
if (income <= 5000) {
return 0;
}
return (income - 5000) * 0.2;
}
}
class StateCouncilSpecialAllowance extends Income {
public StateCouncilSpecialAllowance(double income) {
super(income);
}
@Override
public double getTax() {
return 0;
}
}
totalTax()
方法只需要和Income
打交道,它完全不需要知道Salary
和StateCouncilSpecialAllowance
的存在,就可以正确计算出总的税。如果我们要新增一种稿费收入,只需要从Income
派生,然后正确覆写getTax()
方法就可以。把新的类型传入totalTax()
,不需要修改任何代码。class Person {
public void run() { … }
}
class Student extends Person {
@Override
public void run() { … }
}
class Teacher extends Person {
@Override
public void run() { … }
}
从Person
类派生的Student
和Teacher
都可以覆写run()
方法。
class
定义了方法,但没有具体执行代码,这个方法就是抽象方法,抽象方法用abstract
修饰。学到这,我回头看去,我已经不知道我在学些什么了,我觉得从面对对象编程开始,我就开始迷糊,不知道学的是什么,只是一味的死记住,并不知道怎么用,看着他说的一大片话只是懵懂,有时候看着看着都能发呆,导致这周也没去学些啥,因为不知道在学些啥,我打算要换个方向去学习java了,想去看有讲解的视频去继续学习。
原文:https://www.cnblogs.com/b1356436060/p/13418945.html