package mianxiangduixiangtest;public class DongTaiJingTaitest {public static void main(String[] args) {Animal animal = new Cat();animal.drink();animal.eat();//猫喝水//动物吃饭//即eat方法是与类静态绑定的,在JVM类加载时就确定animal调用的是Animal的eat方法,不需要创建对象//drink方法是与对象动态绑定的,即在运行时才确定drink方法调用的是Cat中的drink方法}
}class Animal{static void eat(){System.out.println("动物吃饭");}void drink(){System.out.println("动物喝水");}
}
class Cat extends Animal{static void eat(){System.out.println("猫吃饭");}void drink(){System.out.println("猫喝水");}
}
package mianxiangduixiangtest;import java.lang.reflect.Method;public class DuoTaiTest {public static void main(String[] args) throws Exception {People p = new Man();//p.eat()过编译,运行时p实际上指向的是子类的结构,故p可以调用子类的方法//输出男人吃饭p.eat();// p.drink(); 不过编译,因为编译只能运行引用类型 类的结构Class aClass = p.getClass();Method drink = aClass.getDeclaredMethod("drink");//输出男人喝水drink.invoke(p);}
}
class People{void eat(){System.out.println("人吃饭");}
}
class Man extends People{void eat(){System.out.println("男人吃饭");}void drink(){System.out.println("男人喝水");}
}
package mianxiangduixiangtest;public class ChouJieTest {
}interface Cup{void Size();
}
abstract class Cup1{//接口可以有默认实现static void Size1(){System.out.println("大尺寸");}abstract void Size();
}
类加载到方法区中(原因:方便JVM找.class文件)
开辟堆内存空间(原因:寻找对象需要的内存空间)
package mianxiangduixiangtest;public class ShengQianCopyTest {public static void main(String[] args) throws Exception{Paper paper = new Paper();Paper clone = (Paper)paper.clone();System.out.println(paper == clone);System.out.println(paper.tree==clone.tree);//浅复制时,第一个结果false,第二个结果为true,说明确实赋复制了新对象,但是对象内部的引用属性为赋值Paper1 paper1 = new Paper1();Paper1 clone1 = paper1.clone();System.out.println(paper1 == clone1);System.out.println(paper1.tree1==clone1.tree1);//深复制时,第一个结果和第二个结果都为false,说明确实复制了新对象,并且对象内部的对象属性也被赋值}
}class Paper implements Cloneable{Tree tree;@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}
}
class Tree{}
class Paper1 implements Cloneable{Tree1 tree1 = new Tree1();@Overrideprotected Paper1 clone() throws CloneNotSupportedException {Paper1 paper1 = (Paper1)super.clone();paper1.tree1 = (Tree1) this.tree1.clone();return paper1;}
}
class Tree1 implements Cloneable{@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}
}
package mianxiangduixiangtest;public class NeiBuLeiTest {public static void main(String[] args) {//静态内部类可以通过外部类.内部类来实例化Person.Inner1 inner1 = new Person.Inner1();//成员内部类必须通过外部类的实例化来实例化Person person = new Person();Person.Inner inner = person.new Inner();}
}class Person{//成员内部类class Inner{}//静态内部类static class Inner1{}//匿名内部类Dog dog = new Dog(){};//局部内部类{class Cat{}}}
class Dog{}