今天发生一些事情,没有动力学太多
很累很难过,希望自己继续努力坚持
-
动态编译:类型:可扩展性
-
即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
-
多态存在的条件
-
有继承关系
-
子类重写父类方法
-
方法重写注意:
staic 方法,属于类,它不属于实例,不可重写
final 常量,不可重写
private 方法,它是私有的,不可重写
-
-
父类引用指向子类对象
-
-
注意:多态是方法的多态,属性没有多态性。
instanceof
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
System.out.println(object instanceof Student); //true
System.out.println(object instanceof Person); //true
System.out.println(object instanceof Object); //true
System.out.println(object instanceof Teacher); //false
System.out.println(object instanceof String); //false
System.out.println("=================================");
Person person = new Student();
System.out.println(person instanceof Student); //true
System.out.println(person instanceof Person); //true
System.out.println(person instanceof Object); //true
System.out.println(person instanceof Teacher); //false
//System.out.println(person instanceof String); 编译直接报错了
System.out.println("=================================");
Student student = new Student();
System.out.println(student instanceof Student); //true
System.out.println(student instanceof Person); //true
System.out.println(student instanceof Object); //true
//System.out.println(student instanceof Teacher); 编译报错
//System.out.println(student instanceof String); 编译报错
父类 对象 = new 子类();
子类 对象1 = (子类)对象; //强制转换
-
父类引用指向子类的对象
-
把子类转换为父类,向上转型
-
把父类转换为子类,向下转型; 需要强制转换
-
方便方法的调用,减少重复的代码!简洁
封装、继承、多态。 抽象,接口
static关键字详解
-
static静态 如果是静态的变量,建议直接用 类.
-
非静态的方法可以直接访问静态的方法
package com.oop.demo06;
public class Person {
// 2:可用来赋初始值
{
System.out.println("匿名代码块");
}
// 1:最先被执行,且只执行一次
static {
System.out.println("静态代码块");
}
// 3:
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("================");
Person person2 = new Person();
}
}
//静态导入包 将 Math 的 random 方法导入,然后可在此类里直接使用
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
System.out.println(random());
}
}
浙公网安备 33010602011771号