【Java学习】Java继承中的细节(8)

Java继承,super,set和get

    • 先写一个小继承代码
    • Java不支持多继承但支持多层继承
    • 静态代码块,构造代码块,构造方法执行顺序
    • 无参构造与有参构造
    • 方法重写,方法复写,方法覆盖
    • super(name, age);
    • 私有成员变量的set与get
    • 利用有参构造对私有成员变量一次性传值

先写一个小继承代码

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Zi zilei = new Zi();
        zilei.fanc();
    }

}
class Fu{
    public void fanc(){
        System.out.println("hello world!");
    }
}
class Zi extends Fu{

}

细节注意:一个代码内只能主类上一个public,其他类上不能写,如果要写,写到同一个包下,另一个java文件中.


Java不支持多继承但支持多层继承

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Zi zilei = new Zi();
        zilei.fanc();
        Grandson grandson = new Grandson();
        grandson.fanc();
    }

}
class Fu{
    public void fanc(){
        System.out.println("hello world!");
    }
}
class Zi extends Fu{

}
class Grandson extends Zi{

}

静态代码块,构造代码块,构造方法执行顺序

静态代码块优先于构造代码块优先于构造方法

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Zi zilei = new Zi();
        zilei.fanc();

    }

}
class Fu{
    public Fu() {
        System.out.println("我是父类无参构造方法");
    }
    {
        System.out.println("我是父类构造代码块");
    }
    static {
        System.out.println("我是父类静态代码块");
        //不论放在那里都是最先执行.
    }

    public void fanc(){
        System.out.println("父类hello world!");
    }
}
class Zi extends Fu{
    {
        System.out.println("我是子类构造代码块");
    }
    static {
        System.out.println("我是子类静态代码块");
        //不论放在那里都是最先执行,但既然继承肯定是父类先执行.
    }

    public Zi() {
        System.out.println("我是子类无参构造方法");
    }
}

我是父类静态代码块
我是子类静态代码块
我是父类构造代码块
我是父类无参构造方法
我是子类构造代码块
我是子类无参构造方法
父类hello world!

Process finished with exit code 0


无参构造与有参构造

什么都不写,默认有无参构造,但是只要写了有参构造,默认的无参构造如果还想用,就必须手动给出!

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Zi zilei = new Zi();//若子类写了有参构造,而不写无参构造,这行就报错.
        System.out.println(zilei.name);//成员变量,引用数据类型默认值为null
        System.out.println(zilei.age);//成员变量,int默认值为0
        zilei.fanc();
        System.out.println("-------------------");
        Zi zilei1 = new Zi("小张",20);//这是调用了子类的有参构造,如果写了父类的有参构造,而不写父类的无参构造就会报错.
        System.out.println(zilei1.name);
        System.out.println(zilei1.age);
        zilei1.fanc();

    }

}
class Fu{
    public String name;
    public int age;
    public Fu() {
        //无参构造,第一行默认为super();调用父类(object)的空参构造,省略不写
    }

    public Fu(String name, int age) {
        //有参构造,第一行默认为super();调用父类(object)的空参构造,省略不写
        this.name = name;
        this.age = age;
    }

    public void fanc(){
        System.out.println("父类的hello world!");
    }
}
class Zi extends Fu{
    public String name;
    public int age;

    public Zi() {
        //无参构造,第一行默认为super();调用父类(Fu)的空参构造,省略不写
    }

    public Zi(String name, int age) {
        //有参构造,第一行默认为super();调用父类(Fu)的空参构造,省略不写
        this.name = name;
        this.age = age;
    }

}

null
0
父类的hello world!
-------------------
小张
20
父类的hello world!

Process finished with exit code 0


方法重写,方法复写,方法覆盖

私有方法不能重写,
重写方法的权限修饰符>=原有权限
静态方法不参与重写
不能重写父类的成员变量

权限规则如下:
public>protected>缺省的>private

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Zi zilei1 = new Zi("小张",20);//这是调用了子类的有参构造,如果写了父类的有参构造,而不写父类的无参构造就会报错.
        zilei1.fanc();
        zilei1.fanc1();

    }

}
class Fu{
    public String name;
    public int age;
    public Fu() {
        //无参构造,第一行默认为super();调用父类(object)的空参构造,省略不写
    }

    public Fu(String name, int age) {
        //有参构造,第一行默认为super();调用父类(object)的空参构造,省略不写
        this.name = name;
        this.age = age;
    }

    public void fanc(){
        System.out.println("我会hello world!");
    }
    public void fanc1(){
        System.out.println("我会开车");
    }
}
class Zi extends Fu{
    public String name;
    public int age;

    public Zi() {
        //无参构造,第一行默认为super();调用父类(Fu)的空参构造,省略不写
    }

    public Zi(String name, int age) {
        //有参构造,第一行默认为super();调用父类(Fu)的空参构造,省略不写
        this.name = name;
        this.age = age;
    }

    @Override
    public void fanc() {
        super.fanc();//延用父类的方法
        System.out.println("我还会继承");//加上自己的内容
    }

    @Override
    public void fanc1() {
        System.out.println("我会写Java");//全部更改
    }
}

我会hello world!
我还会继承
我会写Java

Process finished with exit code 0

super(name, age);

子类有参构造调用父类的有参构造,将父类的name,age改为参数提供的内容

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Zi zilei1 = new Zi("小张",20);//这是调用了子类的有参构造,如果写了父类的有参构造,而不写父类的无参构造就会报错.
        System.out.println("姓名"+zilei1.name+",年龄"+zilei1.age);
        zilei1.fanc();
        zilei1.fanc1();

    }

}
class Fu{
    public String name;
    public int age;
    public Fu() {
        //无参构造,第一行默认为super();调用父类(object)的空参构造,省略不写
    }

    public Fu(String name, int age) {
        //有参构造,第一行默认为super();调用父类(object)的空参构造,省略不写
        this.name = name;
        this.age = age;
    }

    public void fanc(){
        System.out.println("我会hello world!");
    }
    public void fanc1(){
        System.out.println("我会开车");
    }
}
class Zi extends Fu{
    public Zi() {
    }

    public Zi(String name, int age) {
        super(name, age);//调用父类的有参构造,将父类的name,age改为参数提供的内容
    }

    @Override
    public void fanc() {
        super.fanc();//延用父类的方法
        System.out.println("我还会继承");//加上自己的内容
    }

    @Override
    public void fanc1() {
        System.out.println("我会写Java");//全部更改
    }
}

姓名小张,年龄20
我会hello world!
我还会继承
我会写Java


私有成员变量的set与get

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Fu fu = new Fu();
        System.out.println(fu.msg);
        //System.out.println(fu.name);//报错,name为私有属性
        System.out.println("默认值name:"+fu.getName()+",默认值age:"+fu.getAge());
        fu.setAge(20);
        fu.setName("小张");
        System.out.println("赋值后name:"+fu.getName()+",赋值后age:"+fu.getAge());
    }

}
class Fu{
    public String msg = "我是公开的";
    private String name;
    private int 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;
    }
}



我是公开的
默认值name:null,默认值age:0
赋值后name:小张,赋值后age:20

Process finished with exit code 0


利用有参构造对私有成员变量一次性传值

package com.jingfei.csdn;

public class JiCheng {
    public static void main(String[] args) {
        Fu fu = new Fu();//若不给除无参构造,会报错.
        System.out.println(fu.msg);
        //System.out.println(fu.name);//报错,name为私有属性
        System.out.println("默认值name:"+fu.getName()+",默认值age:"+fu.getAge());
        fu.setAge(20);
        fu.setName("小张");
        System.out.println("赋值后name:"+fu.getName()+",赋值后age:"+fu.getAge());
        System.out.println("--------下面利用有参构造传值--------------");
        Fu fu1 = new Fu("小明",22);//利用有参构造传值
        System.out.println("赋值后name:"+fu1.getName()+",赋值后age:"+fu1.getAge());
    }

}
class Fu{
    public String msg = "我是公开的";
    private String name;
    private int age;

    public Fu() {
        //手动给出无参构造
    }

    public Fu(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;
    }
}



我是公开的
默认值name:null,默认值age:0
赋值后name:小张,赋值后age:20
--------下面利用有参构造传值--------------
赋值后name:小明,赋值后age:22

Process finished with exit code 0

----------------------------------------------------------------------------------
谢谢

你可能感兴趣的:(初学Java)