JavaSE 第十七讲 继承剖析 10.4

1.this()

public class ConstructorOverload
{
    public ConstructorOverload()
{
        this(3);

        System.out.println("test");
}

public ConstructorOverload(int i)
{
        System.out.println(++i);
}

public static void main(String[] args)
{
        ConstructorOverload col = new ConstructorOverload();
}
    
}

2.super()

public class Child extends Parent
{
   public Child()
   {
           super(1);
       System.out.println("child");
   }

   public static void main(String[] args)
   {
           Child child = new Child();
   }
}

class Parent
{
   /*
public Parent()
   {
           System.out.println("parent");
   }
   */

public Parent(int i)
{
       System.out.println("parent");
}
}

3.继承特点

public class InheritenceTest
{
  public static void main(String[] args)
  {
          Apple apple = new Apple();

      System.out.println(apple.name);
  }
}

class Fruit
{
    String name = "fruit";
}

class Apple extends Fruit
{
    String name = "apple";
}

你可能感兴趣的:(JavaSE 第十七讲 继承剖析 10.4)