首页 > 其他分享 >继承和多态

继承和多态

时间:2022-10-18 23:16:22浏览次数:69  
标签:Parent 继承 子类 多态 public Child println class

动手实验:继承条件下的构造方法调用

运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大!

 

 

package mysqlconnect;


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 调用基类构造方法,必须是子类构造方法中的第一个语句

 

 

构造函数的功能主要用于在类的对象创建时定义初始化的状态。
构造一个对象,先调用其构造方法,来初始化其成员函数和成员变量。
子类拥有父的成员变量和成员方法,如果不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化。
不能反过来调用也是这个原因,因为父类根本不知道子类有神魔变量而且这样一来子类也得不到初始化的父类变量,导致程序运行出错!

创建“不可变的类”的对象后,此对象的属性不可改,而且也无法从此类派生出新子类。String就是一个典型的例子。 不可变的“类”有何用? 可以方便和安全地用于多线程环境中, 访问它们可以不用加锁,因而能提供较高的性能。


public class ExplorationJDKSource {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new A());
}

}

class A{}

结论:

(1)使用javap –c命令反汇编ExplorationJDKSource.class; (2)阅读字节码指令,弄明白println()那条语句到底调用了什么? (3)依据第(2)得到的结论,使用Eclipse打开JDK源码,查看真正被执行的代码是什么;前面示例中,main方法实际上调用的是: public void println(Object x),这一方法内部调用了String类的valueOf方法。 valueOf方法内部又调用Object.toString方法: public String toString() { return getClass().getName() +"@" + Integer.toHexString(hashCode()); } hashCode方法是本地方法,由JVM设计者实现: public native int hashCode();

 

 

让我们来看一段代码(示例Fruit.java ):

public class Fruit
{

public String toString()
{
return "Fruit toString.";
}

public static void main(String args[])
{
Fruit f=new Fruit();
System.out.println("f="+f);
// System.out.println("f="+f.toString());
}
}

 

 

Fruit类覆盖了Object类的toString方法。

结论: 在“+”运算中,当任何一个对象与一个String对象,连接时,会隐式地调用其toString()方法,默认情况下,此方法返回“类名 @ + hashCode”。为了返回有意义的信息,子类可以重写toString()方法。

 

 

子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

package text;

class Parent
{
    int x;
    public Parent()
    {
        
            System.out.println("Parent Created1");
      }
    public void show(){
        System.out.println("Parent Created2");
    }

}



class Child extends Parent
{
    int y;
    public Child()
     {
    
        System.out.println("Child Created1");

      }
    public void show(){
        super.show();
        System.out.println("Parent Created3");
    }

}



public class text
{


    public static void main(String args[])
     {

            Child c = new Child();
    c.show();
  }

}

Java“方法覆盖”的语法规则

(1)覆盖方法的允许访问范围不能小于原方法。 (2)覆盖方法所抛出的异常不能比原方法更多。 (3)声明为final方法不允许覆盖。 例如,Object的getClass()方法不能覆盖。 (4)不能覆盖静态方法。

型转换”知识点考核-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;

}
}

ava中基类对象不能当做子类对象使用,需要用强制转换来实现,子类对象变量=(子类名)基类对象名;错误的代码是d=m; d=c;

错误原因:类型不匹配:不能从 Mammal 转换为 Dog。

 


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

在实践中理解把握复杂的知识-1

请看以下“变态”的类(参看示例ParentChildTest.java)

 

 

上述代码的特点是: 子类和父类定义了一模一样的字段和方法

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

 

 

总结:

当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。 这个特性实际上就是面向对象“多态”特性的具体表现。

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

在实际开发中,要避免在子类中定义与父类同名 的字段。不要自找麻烦!——但考试除外,考试 中出这种题还是可以的。

标签:Parent,继承,子类,多态,public,Child,println,class
From: https://www.cnblogs.com/zhaoshengfu/p/16804548.html

相关文章