1.继承是面向对象的三大特征之一,三大特征分别是:封装,继承和多态。
2.继承基本的作用是:代码复用。但是“重要的”作用是:有了继承才有了以后“方法的覆盖”和“多态机制”。
3.继承的语法格式:
【修饰符】class 类名 extends 父类名{
类体=属性+方法。
}
4.Java语言当中的继承只支持单继承,一个类不能继承很多类,只能继承一个类。
5.关于继承中的一些术语:
b类继承a类,其中:
a类称为:父类,基类,超类,superclass
b类称为:子类,派生类,subclass
6.子类继承父类的数据:
- 私有的不支持继承
- 构造方法不支持继承
- 其他都可以。
7.Java语言当中只支持单单元继承,但是一个类也可以间接继承其他类,例如:
C extends B{
}
B extends A{
}
A extends T{
}
c类直接继承B类,但是C类简介继承T、类,A类。
8.Java没有显示继承任何类,会自动继承JavaSE库中提供的Java.lang.Object类。
public class Account {
private String actno;
private double balance;
public Account(String actno, double balance) {
this.actno = actno;
this.balance = balance;
}
public Account() {
}
public String getActno() {
return actno;
}
public void setActno(String actno) {
this.actno = actno;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
public class CreditAccount {
private String actno;
private double balance;
private double credit;
public String getActno() {
return actno;
}
public void setActno(String actno) {
this.actno = actno;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
public CreditAccount(String actno, double balance, double credit) {
super();
this.actno = actno;
this.balance = balance;
this.credit = credit;
}
public CreditAccount() {
super();
// TODO Auto-generated constructor stub
}
}
public class CreditAccount extends Account {
private double credit;
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
public CreditAccount(String actno, double balance, double credit) {
super();
this.credit = credit;
}
public CreditAccount() {
super();
// TODO Auto-generated constructor stub
}
}
public class ExtendsTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
CreditAccount a=new CreditAccount();
a.setActno("a-001");
a.setBalance(1000);
a.setCredit(0.99);
System.out.println(a.getActno()+","+a.getBalance()+","+a.getCredit());
}
}