JAVAoooooo

发布时间 2023-10-13 23:56:06作者: 软件拓荒人
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();
  }
}

让我们意料之内的是,爷爷父亲儿子连续继承的时候,先调用爷爷的默认构造函数,在调用父亲的默认构造函数,在调用儿子的构造函数

以final声明的方法不允许覆盖。

以final声明的变量不允许更改。

利用final,可以设计出一种特殊的“只读” 的“不可变类”。

public class ExplorationJDKSource {

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

}

class A{}

此示例中定义了一个类A,它没有任何成员:直接输出这个类所创建的对象

 我们发现打印了一个奇怪的东西

A@1279f2327

前面示例中,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();

在Java中,所有的类都派生自Object

Object是大爹是祖宗的祖宗

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方法。构造函数返回的是一个字符串,然后对象就是调用toString

结论:

在“+”运算中,当任何一个对象与一个String对象,连接时,会隐式地调用其toString()方法,默认情况下,此方法返回“类名 @ + hashCode”。

为了返回有意义的信息,子类可以重写toString()方法。

class Parent 
{
    void message()
    {
        System.out.println("父亲的散文诗");
    }
}

class Child extends Parent 
{

void message()
    {
        System.out.println("孩子的散文诗");
    }
  void test01()
  {
    super.message();
  }
}

public class TestInherits 
{
    public static void main(String args[])
     {

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

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

重写调用了父类的散文诗方法

 

public class TestInstanceof
{
    public static void main(String[] args) 
    {
        
        Object hello = "Hello";
        
        System.out.println("     Object     " + (hello instanceof Object));
          
        System.out.println("     String      " + (hello instanceof String));
          
        System.out.println("     Math       " + (hello instanceof Math));
          
        System.out.println("     Comparable " + (hello instanceof Comparable));
        String a = "Hello";
        
    }
}

 

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;

    }
}

必须有强转换类型,把父类强转为子类