Java学习笔记(十一)

在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第十一篇博客。

本篇博客介绍了Java的继承。

本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11

目录

继承

继承概述

继承的好处和弊端

继承中变量的访问特点

super

继承中构造方法的访问特点

继承中成员方法的访问特点

方法重写

继承的注意事项

猫和狗


继承

继承概述

继承是面向对象的三大特征之一,可以使得子类具有父类的属性和方法,还可以在子类中重新定义,增加属性和方法。

继承的格式是:

public class 子类类名 extends 父类类名{...}

父类又称为基类或超类,子类也称为派生类。extends表示子类继承父类。

子类中可以有自己的内容,也拥有父类的内容。

public class fatherclass {
    public void showfather(){
        System.out.println("A");
    }
}
public class sonclass extends fatherclass{
    public void showson(){
        System.out.println("B");
    }
}

sonclass类继承了fatherclass类,sonclass类不仅有自己的showson方法,还有输入fatherclass类的showfather方法。

public class extendtest {
    public static void main(String[] args){
        fatherclass father = new fatherclass();
        father.showfather();

        sonclass son = new sonclass();
        son.showson();
        son.showfather();
    }
}

这是测试代码,代码中创建了sonclass类的对象son,son方法不仅用了本类的方法,还用了父类的方法。

程序的输出是:

A
B
A

继承的好处和弊端

通过继承,多个类相同的成员可以放到同一个类中,提高了代码的复用性。同时如果有方法的代码需要修改,修改一处即可,提高了代码的维护性。

但是继承让类和类之间产生了关系,类的耦合性增强,父类变化时子类也要发生变化,削弱了子类的独立性。

使用继承时,一般两个类有某种从属关系,如苹果和水果。

继承中变量的访问特点

在子类中访问一个变量,首先在子类局部范围内找,然后在子类成员范围找,然后在父类成员范围找,然后到父类的父类成员范围查找...找不到后报错。

public class fatherclassvariable {
    int age = 50;
    int height = 170;
}
public class sonclassvariable extends fatherclassvariable{
    int age = 20;
    int height = 180;

    public void show(){
        height = 175;
        System.out.println("The height is " + height);
        System.out.println("The age is " + age);
    }
}

父类fatherclassvariable中有age和height变量,子类sonclassvariable中有age和height方法,此类的show方法中有一个局部的height变量。

public class extendvariable {
    public static void main(String[] args) {
        sonclassvariable son = new sonclassvariable();
        son.show();
    }
}

这是测试代码,程序的输出是:

The height is 175
The age is 20
实例化了一个sonclassvariable类对象,并调用show方法,输出的height为show方法内的值,输出的age是sonclassvariable类的成员对象。

super

this代表对本类对象的引用。super代表父类对象的引用。

this.成员变量访问本类的成员变量,this(...)访问本类构造方法,this.成员方法(...)访问本类成员方法。

super.成员变量访问父类成员变量,super(...)访问父类构造方法,super.成员方法(...)访问父类成员方法。

public class fathersuper {
    int age = 50;
}
public class sonsuper extends fathersuper{
    int age = 20;
    public void show(){
        int age = 15;
        System.out.println("The age is " + age);
        System.out.println("The age is " + this.age);
        System.out.println("The age is " + super.age);
    }
}

父类fathersuper中成员变量age的值为50,子类sonsuper中成员变量age的值为20,此类中成员方法show中,age的值为15,show方法依次访问本方法内age变量,本类的成员变量age,父类的成员变量age。

public class extendsuper {
    public static void main(String[] args){
        sonsuper son = new sonsuper();
        son.show();
    }
}

这是测试方法,创建了一个sonsuper类的对象并调用show方法。

程序的输出是:

The age is 15
The age is 20
The age is 50
 

继承中构造方法的访问特点

子类的所有构造方法默认会访问父类的无参构造方法,子类构造方法的第一条语句默认是super()。这是因为子类会继承,也可能使用父类的数据,所以在子类初始化之前要完成父类数据的初始化。

public class fathersupermethod {
    public fathersupermethod(){
        System.out.println("This is a");
    }
    public fathersupermethod(int num){
        System.out.println("This is b");
    }
}

父类fathersupermethod提供两个构造方法,一个无参,输出This is a,一个接受一个int类型参数,并输出This is b。

public class sonsupermethod extends fathersupermethod{
    public sonsupermethod(){
        super(10);
        System.out.println("This is A");
    }
    public sonsupermethod(int num){
        super(10);
        System.out.println("This is B");
    }
}

子类sonsupermethod提供两个构造方法,一个无参,会输出This is A,一个接受一个int类型参数,并会输出This is B。

public class extendsupermethod {
    public static void main(String[] args){
        sonsupermethod son = new sonsupermethod(10);
    }
}

这是测试代码,创建了一个sonsupermethod类对象,并使用了带参数的构造方法。

程序的输出是:

This is b
This is B

sonsupermethod类的构造方法的第一句是super(10),已经表明调用父类的带参构造方法,因此不会访问父类无参构造方法。

如果去掉子类两个构造方法的第一句,程序的输出是:

This is a
This is B

继承中成员方法的访问特点

通过子类对象访问一个方法,先在子类成员内找,然后在父类中,然后在父类的父类中找(如果有)...至找到为止,找不到会报错。

public class fathermethod {
    public void show(){
        System.out.println("This is A");
    }
    public void test(){
        System.out.println("This is B");
    }
}

父类fathermethod中有show方法和test方法,各输出一行语句。

public class sonmethod extends fathermethod{
    public void show(){
        super.show();
        System.out.println("This is a");
    }
}

子类sonmethod中有show方法,先调用父类的show方法,再输出一行语句。这个类本身没有test方法。

public class extendmethod {
    public static void main(String[] args){
        sonmethod son = new sonmethod();
        son.show();
        son.test();
    }
}

这是测试代码,实例化了一个extendmethod类对象,调用show方法和test方法,show方法会使用子类方法,test方法会使用父类方法。

程序的输出是:

This is A
This is a
This is B

方法重写

子类中出现了和父类一样的方法声明,就是方法重写

当子类需要父类的功能,功能主体子类有自己特有内容时,可以重写父类的方法,这样即有父类的功能,又有子类特有的内容。

public class fatheroverwrite {
    public void show(){
        System.out.println("1 + 1 = 2");
    }
}

父类fatheroverwrite的show方法输出1 + 1 = 2

public class sonoverwrite extends fatheroverwrite{
    public void show(){
        System.out.println("1 * 2 = 2");
        super.show();
    }
}

子类sonoverwrite的show方法输出1 * 2 = 2,并调用父类show方法。子类的show方法是方法重写。

public class methodoverwrite {
    public static void main(String[] args){
        sonoverwrite son = new sonoverwrite();
        son.show();
    }
}

这是测试代码。程序的输出是:

1 * 2 = 2
1 + 1 = 2

私有方法不能被重写,子类方法的访问权限不能更低。

继承的注意事项

Java中,类只能单继承,不能多继承(一个类不能同时继承多个类,但是可以同时被多个类继承)。Java的类可以多层继承。

public class teacherstudentextend {
    private String name;
    private int age;
    public teacherstudentextend(){}
    public teacherstudentextend(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 show(){
        System.out.println("The name is " + name);
        System.out.println("The age is " + age);
    }
}

这是学生类和老师类的父类。 

public class teacherextend extends teacherstudentextend{
    public teacherextend(){}
    public teacherextend(String name,int age){
        super(name,age);
    }

    public static void teach(){
        System.out.println("Teacher can teach");
    }
}
public class studentextend extends teacherstudentextend{
    public studentextend(){}
    public studentextend(String name,int age){
        super(name,age);
    }

    public void study(){
        System.out.println("Student can study");
    }
}

teacherextend和studentextend都继承了teacherstudentextend类。这两个类都有只属于自己的方法。

public class teacherstudentextendtest {
    public static void main(String[] args){
        teacherextend teacher1 = new teacherextend();
        teacher1.setname("Dennis");
        teacher1.setage(41);
        teacher1.show();

        teacherextend teacher2 = new teacherextend("James",47);
        teacher2.show();
        studentextend student1 = new studentextend();
        student1.setname("Stephen");
        teacher1.setage(17);
        teacher1.show();

        studentextend student2 = new studentextend("Bruce",19);
        student2.show();

    }
}

这是测试代码,使用了父类的get,set方法或构造方法进行成员变量赋值。

程序的输出是:

The name is Dennis
The age is 41
The name is James
The age is 47
The name is Dennis
The age is 17
The name is Bruce
The age is 19
 

猫和狗

public class catdogextend {
    private String name;
    private int age;
    public catdogextend(){}
    public catdogextend(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 show(){
        System.out.println("The name is " + name);
        System.out.println("The age is " + age);
    }
}

这是猫和狗的父类。

public class dog extends catdogextend{
    public dog(){}
    public dog(String name,int age){
        super(name,age);
    }

}
public class cat extends catdogextend{
    public cat(){}
    public cat(String name,int age){
        super(name,age);
    }
    public void catdo(){
        System.out.println("The cat can eat mouse");
    }
}

这是狗和猫类。

public class catdogextendtest {
    public static void main(String[] args) {
        cat cattest = new cat();
        cattest.setname("Tom");
        cattest.setage(5);
        cattest.show();

        dog dogtest = new dog("Dylon",3);
        dogtest.show();
    }
}

这是测试代码。程序的输出是:

The name is Tom
The age is 5
The name is Dylon
The age is 3
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值