标签:void pack 创建 chm 接口 ret pre set 关键字
1、多态 (面向对象的第三个特性)
(1)多态的前提:必须有父子类关系
package com.oracle.demo01; public class person { public void eat(){ System.out.println("我吃的不多"); } }
package com.oracle.demo01; public class student extends person{ }
package com.oracle.demo01; public class test { public static void main(String[] args) { //多态:将子类指向父类引用 person p = new student(); } }
其他情况也一样:
public class Test { public static void main(String[] args) { //1.父类,子类都是普通类的多态 //将一个student对象赋值给Student引用变量 Student s=new Student(); //多态的前提:必须有子父类关系 //将一个student对象赋值给Person引用变量 //多态:子类对象指向父类引用 Person p=new Student(); //2.父类是抽象类,子类是普通类的多态 Animal a=new Cat(); //3.父类是接口,子类是普通类的多态 Smoking smoke=new Man(); } }
(2)父类引用调用成员变量的是父类,调用成员方法,调的是子类重写后的方法
package com.oracle.demo01; public class person { int a = 1; public void eat(){ System.out.println("我吃的不多"); } }
package com.oracle.demo01; public class student extends person{ int a = 2; public void eat(){ System.out.println("我吃的贼多"); } }
package com.oracle.demo01; public class test2 { public static void main(String[] args) { //调用的是子类重写的方法 person p = new student(); p.eat(); //我吃的贼多 System.out.println(p.a);//1 //父类引用调用成员变量的是父类,调用成员方法,调的是子类重写后的 方法 } }
(3)关键字instanceof:判断某个对象是否属于某种数据类型;boolean b = 对象 instanceof 数据类型;
举个例子:
package com.oracle.demo01; public abstract class Animal { public abstract void eat(); }
package com.oracle.demo01; public class Cat extends Animal{ public void eat() { System.out.println("猫吃猫粮"); } }
package com.oracle.demo01; public class Teacher extends Person{ }
判断创建的动物对象是否是老师类
package com.oracle.demo01; public class Test2 { public static void main(String[] args) { Animal a=new Cat(); a.eat(); boolean flag = a instanceof Teacher; System.out.println(flag);//false } }
判断创建的动物是否是猫类
package com.oracle.demo01; public class Test2 { public static void main(String[] args) { Animal a=new Cat(); a.eat(); boolean flag = a instanceof Teacher; System.out.println(flag);//true } }
(4)转型:
向上转型【多态】[提高拓展性];
向下转型【需要用到关键字instanceof】【同时先必须要有向上转型】
【直接创建父类对象,是无法向下转型的!】[子类特有功能]
package com.oracle.demo02; public abstract class Animal { public abstract void eat(); }
package com.oracle.demo02; public class Cat extends Animal{ public void eat() { System.out.println("猫吃鱼"); } public void catchMouse(){ System.out.println("猫抓老鼠"); } }
package com.oracle.demo02; public class Dog extends Animal{ public void eat() { System.out.println("狗吃肉"); } public void lookHome(){ System.out.println("狗看家"); } }
package com.oracle.demo02; import java.util.ArrayList; import com.oracle.demo01.Person; import com.oracle.demo01.Student; public class Test { public static void main(String[] args) { //向上转型 Animal cat=new Cat(); Animal dog=new Dog(); cat.eat(); dog.eat(); //向下转型 if(cat instanceof Cat){ Cat c=(Cat)cat; c.catchMouse(); } if(dog instanceof Dog){ Dog d=(Dog)dog; d.lookHome(); } } }
注意:直接创建父类对象情况
package com.oracle.demo01; public class person { public void eat(){ System.out.println("我吃的不多"); } }
package com.oracle.demo01; public class student extends person{ public void eat(){ System.out.println("我吃的贼多"); } }
package com.oracle.demo01; public class test { public static void main(String[] args) { person p = new person(); student s = (student)p; s.eat(); } }
会显示错误:
第七行为:
2、构造方法:
(1)权限 类名(参数列表){}
package com.oracle.demo04; //构造方法 //权限 类名(参数列表){ // //} public class Person { private String name; private Person(){ } }
(2)创建对象的时候被调用,而且一个对象只能调用一次构造方法,构造方法只能对属性赋值一次
举个例子
package com.oracle.demo02; public class person { String name; public person(String name){ this.name = name; } }
package com.oracle.demo02; public class test { public static void main(String[] args) { person p = new person("lisi"); System.out.println(p.name); } }
与set进行比较:
package com.oracle.demo02; public class person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.oracle.demo02; public class test { public static void main(String[] args) { person p = new person(); p.setName("lisi"); System.out.println(p.getName()); } }
(3)当一个类没有构造方法时,系统默认提供一个空参构造;
当一个类有构造方法时,系统不会提供空参构造
(4)可以再通过set方法进行改值
标签:void pack 创建 chm 接口 ret pre set 关键字
原文地址:https://www.cnblogs.com/scw123/p/9698835.html