[java基础]面向对象(二)

继承:

  • 作用:代码复用
  • 通过extends实现继承
  • 超类/基类/父类:共有的属性和行为
  • 派生类/子类:特有的属性和行为  
  • 派生类可以访问:超类的+派生类的,超类不能访问派生类的

  • 一个超类可以有多个派生类,一个派生类只能有一个超类---------单一继承

  • 具有传递性

  • java规定:构造派生类之前必须先构造超类

  1. 在派生类的构造方法中若没有调用超类的构造方法,则默认super()调用超类的无参构造方法

  2. 在派生类的构造方法中若自己调用了超类的构造方法,则不再默认提供

注意:super()调用超类构造方法,必须位于派生类构造方法中的第1行

public class Person {
    String name;
    int age;
    String address;
    Person(String name,int age,String address){
        this.name = name;
        this.age = age;
        this.address = address;
    }

    void eat(){
        System.out.println(name+"正在吃饭...");
    }
    void sleep(){
        System.out.println(name+"正在睡觉...");
    }
    void sayHi(){
        System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
    }
}

public class Student extends Person{
    String className;
    String stuId;
    Student(String name,int age,String address,String className,String stuId){
        super(name,age,address); //传递的是name/age/address的值
        this.className = className;
        this.stuId = stuId;
    }

    void study(){
        System.out.println(name+"正在学习...");
    }
}

public class Teacher extends Person{
    double salary;
    Teacher(String name,int age,String address,double salary){
        super(name,age,address);
        this.salary = salary;
    }

    void teach(){
        System.out.println(name+"正在讲课...");
    }
}

public class Doctor extends Person {
    String title;
    Doctor(String name,int age,String address,String title){
        super(name,age,address);
        this.title = title;
    }
    void cut(){
        System.out.println(name+"正在做手术...");
    }
}

public class ExtendsTest {
    public static void main(String[] args) {
        Student zs = new Student("张三",25,"廊坊","jsd2302","001");
        zs.eat();
        zs.sleep();
        zs.sayHi();
        zs.study();

        Teacher ls = new Teacher("李四",35,"佳木斯",6000.0);
        ls.eat();
        ls.sleep();
        ls.sayHi();
        ls.teach();

        Doctor ww = new Doctor("王五",46,"山东","主任医师");
        ww.eat();
        ww.sleep();
        ww.sayHi();
        ww.cut();
    }
}

super:指代当前对象的超类对象

  • super.成员变量名-------------------------访问超类的成员变量

    当超类成员变量和派生类成员变量同名时,super指超类的,this指派生类的

    若没有同名现象,则写super和this是一样的

  • super.方法名()------------------------------调用超类的方法

  • super()----------------------------------------调用超类的构造方法

public class SuperDemo {
    public static void main(String[] args) {
        Boo o = new Boo();
    }
}


//在派生类的构造方法中若自己调用了超类的构造方法,则不再默认提供
class Coo{
    Coo(int a){
    }
}
class Doo extends Coo{
    Doo(){
        super(5);
    }
    /*
    //如下代码为默认的
    Doo(){
        super();
    }
     */
}

class Aoo{
    Aoo(){
        System.out.println("超类构造方法");
    }
}
class Boo extends Aoo{
    Boo(){
        super(); //默认的,调用超类的无参构造方法
        System.out.println("派生类构造方法");
    }
}

方法的重写(override/overriding):重新写、覆盖

  • 发生在父子类中,方法名相同,参数列表相同

  • 重写方法被调用时,看对象的类型---------------new谁就调谁的,这是规定

class 餐馆{
    void 做餐(){ 做中餐 }
}
//1)我还是想做中餐----------------------不需要重写
class Aoo extends 餐馆{
}
//2)我想改做西餐------------------------需要重写
class Boo extends 餐馆{
    void 做餐(){ 做西餐 }
}
//3)我想在中餐基础之上加入西餐------------需要重写(先super中餐,再加入西餐)
class Coo extends 餐馆{
    void 做餐(){
        super.做餐();
        做西餐
    }
}

例子:

public class Animal {
    String name;
    int age;
    String color;
    Animal(String name,int age,String color){
        this.name = name;
        this.age = age;
        this.color = color;
    }
    void drink(){
        System.out.println(color+"色的"+age+"岁的"+name+"正在喝水...");
    }
    void eat(){ //---------------明天继续改造
        System.out.println(color+"色的"+age+"岁的"+name+"正在吃饭...");
    }
}

public class Dog extends Animal {
    Dog(String name,int age,String color){
        super(name,age,color);
    }
    void lookHome(){
        System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在看家...");
    }
    void eat(){
        System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在吃肯头...");
    }
}

public class Chick extends Animal {
    Chick(String name,int age,String color){
        super(name,age,color);
    }
    void layEggs(){
        System.out.println(color+"色的"+age+"岁的小鸡"+name+"正在下蛋...");
    }
    void eat(){
        System.out.println(color+"色的"+age+"岁的小鸡"+name+"正在吃小米...");
    }
}

public class Fish extends Animal {
    Fish(String name,int age,String color){
        super(name,age,color);
    }
    void eat(){
        System.out.println(color+"色的"+age+"岁的小鱼"+name+"正在吃小虾...");
    }
}

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog("小黑",2,"黑");
        dog.eat();
        dog.drink();
        dog.lookHome();

        Chick chick = new Chick("花花",3,"棕");
        chick.eat();
        chick.drink();
        chick.layEggs();

        Fish fish = new Fish("金金",1,"金");
        fish.eat();
        fish.drink();
    }
}

注:

  1. 泛化:从程序设计角度而言叫泛化,从代码实现角度而言叫继承,泛化就是继承
  2. 继承要符合is(是)的关系
  3. 继承的是超类的成员变量和普通方法,不包括超类的构造方法,超类的构造方法是被派生类通过super()来调用的,而不是继承的
class Aoo{
    int a;
    Aoo(){
    }
    void show(){
    }
}
class Boo extends Aoo{
    继承了Aoo类的a+show(),并没有继承Aoo类的构造方法
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值