对象转型(casting); 多态; static(静态);单例模式
对象转型:
public class Animals { //父类 private String lei; private double weight; //需要get,set一下,外界才可以调用 public Animals() { } public Animals(String lei, double weight) { super(); this.lei = lei; this.weight = weight; } public void Print() { System.out.println("动物种类" + lei); System.out.println("重量" + weight); } /*public void Sv () { System.out.println("你好"); }*/ public String getLei() { return lei; } public void setLei(String lei) { this.lei = lei; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
public class Dog extends Animals { //子类 private String shi; public Dog() { } public Dog(String lei, double weight,String shi) { super(lei, weight); //引用的是父类里的lei,weight构造方法 this.shi = shi; } public void Print () { //重写父类里的方法 System.out.println("动物种类是" + super.getLei()); //因为是继承,super之后就可以获取 System.out.println("重量" + super.getWeight()); System.out.println("狗的食物"+ shi); } public String getShi() { return shi; } public void setShi(String shi) { this.shi = shi; } }
public class Main { public static void main(String[] args) { //父类的引用指向子类对象 Animals an = new Dog(); //实例化Animals an.setLei("狗"); an.setWeight(15); /*an.Print();*///父类里的方法,要想设置食物,走下一步强转 Dog dg = (Dog) an; //子类Dog新增的属性shi和方法,不能直接访问,要强转 dg.setShi("骨头"); dg.Print(); } }
效果:
多态:
public class Test {
public void Printf(Animals an) { //多态 if(an instanceof Dog) { //如果an这个对象是一种狗, //instanceof关键字是用来判断变量an是不是这个类或是这个类的子类 Dog x =(Dog)an; //强转成狗 x.Print(); //输出Dog的构造方法 } else if(an instanceof Cat ) { Cat c = (Cat)an; //强转成猫 c.Print();//输出Cat的构造方法 }else { an.Print(); //animals的方法 } }
}
//多态 Dog dog = new Dog("狗",15,"骨头"); //先实例化一个Dog Cat cat = new Cat("猫",5,"老鼠"); Test t = new Test(); t.Printf(dog); //dog就是an的一种 t.Printf(cat);
静态(static):
public class Dog extends Animals { //子类 //static 静态 private String name; private int age; public static int num; //static public Dog() { //给num一个构造方法 num++; } public void Print () { System.out.println("这是第" + num + "只狗"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
//static 的静态利用 Dog d1 = new Dog(); d1.Print(); //输出第几只狗 System.out.println(Dog.num); //static 直接拿类名 . num 输出,输出第一个狗 Dog d2 = new Dog(); System.out.println(Dog.num); //输出第二只狗 d2.Print();
单例模式:
public class HelloWorld { private static HelloWorld helloworld; private HelloWorld () { } private int count; public static HelloWorld getinstance() { //static的不用实例化,直接用 if (helloworld == null ) { //如果helloworld是空的话 helloworld = new HelloWorld(); //给helloworld实例化 } return helloworld; //返回helloworld } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
//单例模式 HelloWorld hw = HelloWorld.getinstance(); System.out.println(hw.getCount()); //输出的返回值是为0,因为没有给其赋值 HelloWorld hw2 = HelloWorld.getinstance(); hw2.setCount(5); System.out.println( hw.getCount());*///输出的是5,因为hw与hw2指向的位置相同