首页 > 其他分享 >动手动脑——继承和多态

动手动脑——继承和多态

时间:2022-10-11 09:35:44浏览次数:57  
标签:String parent 子类 动脑 多态 printValue 动手 父类 public

动手实验

 

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();
    
  }

}

 

 

输出结果:

 

结论:

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

 

思索:

为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来?

构造函数(constructor)是一种特殊的方法 。主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 。特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载。构造函数的功能主要用于在类的对象创建时定义初始化的状态。

 

判断对象是否可以转换?

public class TestInstanceof
{
    public static void main(String[] args) 
    {
        //声明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));
    }
}

 

运行结果:

 

 

 

 2.

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;

    }
}

 

不能运行原因:

Dog类和Cat类不能互相转换。

 

在实践中把握复杂知识-2

 

public class ParentChildTest {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();
        
        parent=child;
        parent.printValue();
        
        parent.myValue++;
        parent.printValue();
        
        ((Child)parent).myValue++;
        parent.printValue();
        
    }
}

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

 

运行结果:

 

 

 

 

原因:

Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();

输出相对应的Value数值。

 

 parent=child;
        parent.printValue();

将子类赋值给父类,输出相对应的子类Value:200.

 

parent.myValue++;
        parent.printValue();

 

parent.myValue++;说明修改的是父类,而结果中输出的是子类,子类未修改,输出对应Value:200.

((Child)parent).myValue++;
        parent.printValue();

 

将parent父类强制转换成child子类,myValue++,为101,子类输出为201.

总结:

当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。
如果子类被当作父类使用,则通过子类访问的字段是父类的!

 

标签:String,parent,子类,动脑,多态,printValue,动手,父类,public
From: https://www.cnblogs.com/JJTyyds/p/16778118.html

相关文章

  • 实战项目-用winform动手写一个计算器
    实战项目-用winform动手写一个计算器实战项目--用winform动手写一个“计算器”实战项目旨在交流学习,欢迎在评论区交流意见。简介:分享一个简单的软件项目---自己动手写一个......
  • 继承与多态中动手动脑
    -子类自动拥有父类声明为public和protected的成员,这就是继承特性的体现之一。-public:外界可自由访问-private:外界不可访问-protected:同一包中的子类都可以访问,另一包......
  • 动手动脑
    引用类型vs原始数据类型“引用”一个对象的变量称为“引用类型”的变量,有时又简称为“对象变量”。例:MyClassobj=newMyClass();诸如int,float之类的变量称为“原始......
  • 动手动脑:方法覆盖
    注:方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)测试:1.  在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。packagetest2;classGrandp......
  • 动手实验:继承条件下的构造方法调用
    packagetest2;classGrandparent{publicGrandparent() { System.out.println("GrandParentCreated."); }publicGrandparent(String......
  • 自己动手写ls命令——Java版
    自己动手写ls命令——Java版介绍在前面的文章Linux命令系列之ls——原来最简单的ls这么复杂当中,我们仔细的介绍了关于ls命令的使用和输出结果,在本篇文章当中我们用Java代......
  • 多态
    多态 多态机制的实现就是通过虚函数。 虚函数的原理  虚函数表单个类的虚函数表以上结论通过以下代码测试:运行结果: 证明内存就是如上分布。通过调用命......
  • 多态的优势和弊端
    在多态形式下,右边对象可以实现解耦合,便于扩展和维护personp=newStudent();定义方法的时候,使用父类型作为参数,可以接收所有子类对象,体现多态的扩展性与便利。1.多态的优......
  • Java封装、继承、多态、抽象、接口基础知识
    Java封装、继承、多态、抽象、接口基础知识封装面向对象三大特性:封装、继承、多态(四大特性则多一个抽象)定义把对象的属性和方法结合为一个独立的整体,并尽可能隐藏对......
  • Java语言中多态的机制
    1.方法解析Class文件的编译过程中,不包含传统编译中的连接步骤,一切方法的调用在Class文件中存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址。这个特性......