栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自学JAVA笔记06(基础篇)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自学JAVA笔记06(基础篇)

方法重写

重写:需要有继承关系,子类重写父类的方法!

方法名必须相同;参数列表必须相同;修饰符:范围可以扩大,但不能缩小:public>protected>Default>private抛出的异常:异常范围可以被缩小,但不能扩大:ClasNotFouException–>Excetion(大)子类的方法和父类必须一致,方法体不相同!重写都是方法的重写和属性无关

主程序

public class Application {
    public static void main(String[] args) {
        Son a = new Son();
        a.print();
        //父类的引用指向了子类
        Father b = new Son();//子类重写了父类的方法
        b.print();
    }
}

父类

public class Father {
    protected void print(){//可以尝试去加static,对比静态方法有何不同
        System.out.println("father->test");
    }
}

子类

public class Son extends Father{
    public void print(){
        System.out.println("son->test");
    }
}

加了static的结果,方法没有被重写

静态方法和非静态方法

静态方法:无法被重写,只能被继承,或被子类重新声明。非静态方法:可以被重写 为什么需要重写

父类的功能不一定满足子类的需求

例如定义了动物类(有个动物的叫声),现在有个猫类,他的叫声时喵喵,这时可以用重写方法,来满足子类的需求。 子类不一定需要 多态

同一方法可以根据发送对象的不同而采用多种不同的行为方法一个对象的实际类型时确定的,但可以指向对象的引用的类型有很多多态存在的条件:

有继承关系子类重写父类的方法父类引用指向子类对象

主程序

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的,但是指向的引用类型时不确定的:父类指向子类
        Son s1 = new Son();//Son能调用的方法都是自己的或者继承父类的
        Father s2 = new Son();//Father 父类,可以指向子类,但是不能调用子类独有的方法
        Object s3 = new Son();//Object ‘祖宗类’,可以指向自己的儿孙类。
        //对象能执行方法主要看对象左边的类型,与右边关系不大
        s2.run();//子类重写了父类的方法
        s1.run();
    }
}

父类

public class Father {
    public void run(){
        System.out.println("Father run");
    }
}

子类

public class Son extends Father{
    public void run(){
        System.out.println("Son run");
    }
    public void crawl(){
        System.out.println("Son crawl");
    }
}

运行结果

s2可调用的方法,可以看到没有子类的crawl,因为这是子类的独有的,但是它可以调用run方法,因为他们是共有的,子类只是进行了重写。

多态的注意事项

多态是方法的多态,属性没有多态;父类和子类,有联系,类型转换异常;存在的条件。 无法重写的情况

static 方法,属于类,它不属于实例;final ,被该修饰符修饰的都是属于常量;private 方法,私有方法。 多态 (instanceof)

主程序(父类:father,子类:Son、Daughter)

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Father > Son
        //Object > Father > Daughter
        Object object = new Son();
        System.out.println(object instanceof Son);//true
        System.out.println(object instanceof Father);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Daughter);//false
        System.out.println(object instanceof String);//false
        System.out.println("*****************************");
        Father father = new Son();
        System.out.println(father instanceof Son);//true
        System.out.println(father instanceof Father);//true
        System.out.println(father instanceof Object);//true
        System.out.println(father instanceof Daughter);//false
        //System.out.println(father instanceof String);编译报错,father和String相当于是兄弟,没有继承关系
        System.out.println("*****************************");
        Son son = new Son();
        System.out.println(son instanceof Son);//true
        System.out.println(son instanceof Father);//true
        System.out.println(son instanceof Object);//true
        //System.out.println(son instanceof Daughter);//编译报错
        //System.out.println(son instanceof String);//编译报错
    }
}

能否编译成功看类,也就是在我们创建对象的时候左边部分

以 System.out.println(x instanceof y); 来理解,x与y有父子或者是祖孙(继承)关系或者是本类的才能编译成功

编译成功后,T/F看引用指向的对象,如果x指向的对象是后面y的子类对象或者本类对象则为T;

类之间的转换

父类引用指向子类的对象把子类转换为父类,向上转型(会丢失子类独有的方法)把子类转换为父类,向下转型:强制转换

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755940.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号