如何实现继承
- 运用extends关键字实现继承
- 编写父类
class Animal{//公共的属性和方法}
- 编写子类,继承父类
class Dog extends Animal{//子类特有的属性和方法}
class Cat extends Animal{}
- 只能继承一个父类
- 子类可以访问父类非私有成员
- 父类不可以访问子类特有成员
方法重写
- 语法规则
- 返回值类型
- 方法名
- 参数类型、顺序、个数
- 都要与父类继承的方法相同
- 方法返回值可以允许是子类类型
- 与方法的参数名无关
- 当子类重写父类方法后,子类对象调用的是重写后的方法
- 访问修饰符,访问范围需要大于等于父类的访问权限
继承与方法重写的案例
public class Work {
// 属性:工作名
private String name ;
// 无参构造方法
public Work(){
}
// 带参构造方法,完成工作类型的赋值
public Work(String name){
this.setName(name);
}
// 公有的get***/set***方法完成属性封装
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
// 方法:工作描述,描述内容为:开心工作
public String work() {
String str = "开心工作";
return str;
}
}
public class TestWork extends Work {
//属性:编写的测试用例个数、发现的Bug数量
private int testCount;
private int testBug;
// 编写构造方法,并调用父类相关赋值方法,完成属性赋值
public TestWork(String name,int testCount,int testBug){
this.setName(name);
this.setTestCount(testCount);
this.setTestBug(testBug);
}
public TestWork(){
}
// 公有的get***/set***方法完成属性封装
public void setTestCount(int testCount){
this.testCount=testCount;
}
public int getTestCount(){
return testCount;
}
public void setTestBug(int testBug){
this.testBug=testBug;
}
public int getTestBug(){
return testBug;
}
// 重写运行方法,描述内容为:**的日报是:今天编写了**个测试用例,发现了**bug。其中**的数据由属性提供
public String work() {
String str = this.getName()+"的日报是,今天编写了"+this.getTestCount()+"测试用例,发现了"+this.getTestBug()+"个Bug.";
return str;
}
}
public class DevelopmentWork extends Work {
// 属性:有效编码行数、目前没有解决的Bug个数
private int codeRow;
private int bug;
//编写构造方法,并调用父类相关赋值方法,完成属性赋值
public DevelopmentWork(){
}
public DevelopmentWork(String name,int codeRow,int bug){
this.setCodeRow(codeRow);
this.setBug(bug);
this.setName(name);
}
// 公有的get***/set***方法完成属性封装
public void setCodeRow(int codeRow){
this.codeRow=codeRow;
}
public int getCodeRow(){
return this.codeRow;
}
public void setBug(int bug){
this.bug=bug;
}
public int getBug(){
return this.bug;
}
// 重写运行方法,描述内容为:**的日报是:今天编写了**行代码,目前仍然有**个bug没有解决。其中**的数据由属性提供
public String work() {
String str = this.getName()+"的日报是:今天编写了"+this.getCodeRow()+"行代码,目前仍然有"+this.getBug()+"个bug没有解决.";
return str;
}
}
public class Test {
public static void main(String[] args) {
Work w = new Work();
TestWork tw = new TestWork("测试工作",10,5);
DevelopmentWork dw = new DevelopmentWork("研发工作",1000,10);
System.out.print("父类信息测试:");
System.out.println(w.work());
System.out.print("测试工作类信息测试:");
System.out.println(tw.work());
System.out.print("研发工作类信息测试:");
System.out.println(dw.work());
}
}
访问修饰符的分类及作用
super关键字
- 子类访问父类成员
- 访问父类成员方法
super.print();
- 访问父类属性
super.name;
- 子类的构造的过程中必须调用其父类的构造方法
- 如果子类的构造方法中没有显式标注,则系统默认调用父类无参的构造方法
- 如果子类构造方法中既没有显式标注,且父类中没有无参的构造方法,则编译出错
- 使用super调用父类指定构造方法,必须在子类的构造方法的第一行
继承的初始化顺序
总结案例
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;//品牌
private String color;//颜色
private int wheelNum=2;//轮子默认2个
private int seatNum=1;//座椅默认1个
// 无参构造方法
public NonMotor(){
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand,String color){
this.setBrand(brand);
this.setColor(color);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String brand,String color,int wheelNum,int seatNum){
this.setBrand(brand);
this.setColor(color);
this.setWheelNum(wheelNum);
this.setSeatNum(seatNum);
}
// 公有的get***/set***方法完成属性封装
public void setBrand(String brand){
this.brand=brand;
}
public String getBrand(){
return brand;
}
public void setColor(String color){
this.color=color;
}
public String getColor(){
return color;
}
public void setWheelNum(int wheelNum){
this.wheelNum=wheelNum;
}
public int getWheelNum(){
return wheelNum;
}
public void setSeatNum(int seatNum){
this.seatNum=seatNum;
}
public int getSeatNum(){
return seatNum;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的非机动车,有"+this.getWheelNum()+"个轮子,有"+this.getSeatNum()+"个座椅的非机动车。";
return str;
}
}
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(){
}
public Bicycle(String brand,String color){
super(brand,color);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work(){
String str="这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的自行车。";
return str;
}
}
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String batteryBrand;
// 公有的get***/set***方法完成属性封装
public ElectricVehicle(){
}
public ElectricVehicle(String batteryBrand){
this.setBatteryBrand(batteryBrand);
}
public void setBatteryBrand(String batteryBrand){
this.batteryBrand=batteryBrand;
}
public String getBatteryBrand(){
return batteryBrand;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work(){
String str="这是一辆使用"+this.getBatteryBrand()+"牌电池的电动车。";
return str;
}
}
public class Test {
public static void main(String[] args) {
NonMotor non=new NonMotor("天宇","红",4,2);
System.out.println("父类信息测试:"+non.work());
Bicycle bic=new Bicycle("捷安特","黄");
System.out.println("自行车类信息测试:"+bic.work());
ElectricVehicle EleVeh=new ElectricVehicle("飞鸽");
System.out.println("电动车类信息测试:"+EleVeh.work());
Tricycle tri=new Tricycle();
System.out.println("三轮车类信息测试:"+tri.work());
}
}