首页 > 编程语言 >java第四讲-继承与多态-InheritsAndPolymorphismSourceCode

java第四讲-继承与多态-InheritsAndPolymorphismSourceCode

时间:2022-10-22 16:24:23浏览次数:47  
标签:java String class InheritsAndPolymorphismSourceCode System 多态 println public out

1.继承条件下类的访问权限

public: 外界可自由访问;

private: 外界不可访问;

protected: 同一包中的子类都可以访问,另一包中的子类(派生于同一个父类)也可以访问;

default: 如果不指明任何权限,则默认同一包中的类可以访问;

2.通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句。

class Grandparent
{
    public Grandparent()
    {
        System.out.println("GrandParent Created.");
    }
    public Grandparent(String string)
    {
        System.out.println("GrandParent Created.String:" + string);
    }
}
class Parent extends Grandparent
{
    public Parent()
    {
        //super("Hello.Grandparent.");
        System.out.println("Parent Created");
        // super("Hello.Grandparent.");
    }
}
class Child extends Parent
{
    public Child()
    {
        System.out.println("Child Created");
    }
}
public class TestInherits
{
    public static void main(String args[])
    {
        Child c = new Child();
    }
}

运行结果:

 

 

 如果在 Parent()方法 里写super("Hello.Grandparent.");注意:一定是在第一条语句,不然会报错

运行结果:

 

 

 3.Java方法覆盖语法规则

(1)覆盖方法的允许访问范围不能小于原方法。

(2)覆盖方法所抛出的异常不能比原方法更多。

(3)声明为final方法不允许覆盖。 例如,Object的getClass()方法不能覆盖。

(4)不能覆盖静态方法。

4.判断对象是否可以转换:

可以使用instanceof运算符判断一个对象是否可以转换为指定的类型

注意代码的最后两行,被注释的两行

Object obj="Hello";
if(obj instanceof String)
    System.out.println("obj对象可以被转换为字符串");
//声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
        //但hello变量的实际类型是String
        Object hello = "Hello";
        //String是Object类的子类,所以返回true。
        System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
        //返回true。
        System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
        //返回false。
        System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
        //String实现了Comparable接口,所以返回true。
        System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
        String a = "Hello";
        //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
//        System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));

5.类型转换

class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}

public class TestCast
{
    public static void main(String args[])
    {
        Mammal m;
        Dog d=new Dog();
        Cat c=new Cat();
        m=d;
        //d=m;
        d=(Dog)m;
        //d=c;
        c=(Cat)m;
    }
}

6.多态,继承

package InheritsAndPolymorphismSourceCode;

public class ParentChildTest {
    public static void main(String[] args) {
        Parent1 parent = new Parent1();
        parent.printValue();
        Child1 child = new Child1();
        child.printValue();

        parent = child;
//        System.out.println(parent.myValue);//100
        parent.printValue();

        parent.myValue++;//基类的myValue值
//        System.out.println(parent.myValue);//101
        parent.printValue();

        ((Child1)parent).myValue++;
//        System.out.println(parent.myValue);//101
        parent.printValue();
    }
}
class Parent1{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child1 extends Parent1{
    public int myValue=200;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}

要先parent=child才能写((Child1)parent).myValue++;不然会报错

运行结果

 

另一个例子

class Parent2
{
    public int value=100;
    public void Introduce()
    {
        System.out.println("I'm father");
    }
}

class Son extends Parent2
{
    public int value=101;
    public void Introduce()
    {
        System.out.println("I'm son");
    }
}
class Daughter extends Parent2
{
    public int value=102;
    public void Introduce()
    {
        System.out.println("I'm daughter");
    }
}

public class TestPolymorphism
{
    public static void main(String args[])
    {
        Parent2 p=new Parent2();
        p.Introduce();//I'm father
        System.out.println(p.value);//100
        p=new Son();//p指向son
        p.Introduce();//I'm son
        System.out.println(p.value);//仍然是他自己本身的value即100,除非类型转换
        p=new Daughter();//指向Daughter
        p.Introduce();//I'm daughter
        System.out.println(p.value);//仍然是自己的value即100
    }
}

运行结果:

7.抽象类

标签:java,String,class,InheritsAndPolymorphismSourceCode,System,多态,println,public,out
From: https://www.cnblogs.com/hmy22466/p/16816336.html

相关文章

  • java---泛型(Generics)
    泛型是JDK1.5以后增加的,它可以帮助我们建立类型安全的集合。什么是泛型泛型的本质就是“数据类型的参数化”,处理的数据类型不是固定的,而是可以作为参数传入,可以把“泛......
  • 【JavaScript】事件的冒泡,委派,绑定和传播
    文章目录​​冒泡事件​​​​特性:​​​​阻止冒泡事件的两种方法​​​​cancelBubble​​​​stopPropagation()​​​​委派事件​​​​特性​​​​获取点击的元素​......
  • java SPI机制与双亲委派机制的不同
     1、双亲委派机制1.1定义当一个类加载器收到了类加载的请求的时候,他不会直接去加载指定的类,而是把这个请求委托给自己的父加载器去加载。如果父类为空,交给boot......
  • java第三讲-类与对象-ClassAndObjectSourceCode
    1.通过对象变量使用对象  1)直接调用类的方法;(添加和调用get()方法取值)  2)存取类的字段;(设置公有字段)publicclassClassAndObjectTest{publicstaticvoidm......
  • Java的接口和抽象类到底哪个更抽象
    Java的接口和抽象类到底哪个更抽象抽象类抽象方法是是不完整的方法,,它只是进行了声明,没有具体的方法实现逻辑,具体的实现逻辑由子类来完成。包含抽象方法的类是抽象类,通过ab......
  • Java不能继承多个类?内部类帮你解决这个问题
    Java不能继承多个类?内部类帮你解决这个问题内部类在Java中也是一个很重要的概念,很多类中都存在内部类。内部类与内部类的对应的是外围类,内部类可以操作外围类的所有成员,p......
  • 【Java SE】枚举类和注解
    1.枚举类的使用当类的对象由有限个,确定的时候,我们称这种类为枚举类。当需要定义一组常量时,建议使用枚举类。而当枚举类中只有一个对象时,可以使用单例模式。1.1enmu关键......
  • java线程的优先级
    packageA_ShangGuiGu.Thread.ThreadDemo;/***优先级*1.MAX_Priority-----最大优先级为10*2.MIN_Priority-----最小优先级为1*3.NORM_Priority----默认优先级5*4......
  • java基础-->注释
    注释注释:注释起到对代码标注和解释的作用,如果你去看看JDK源码,会发现他们有许多的注释,而且注释是比代码还要多的,可见为代码添加注释是非常重要的,写好注释能让别人更加容易......
  • java第六讲-StreamAndFileSourceCode
    1.PrintStream学习:Java打印流PrintStream类详解_风有点大的博客-CSDN博客_printstream类1.1向文件进行输出PrintStreamps=newPrintStream("helloworld01.txt"......