一台服务做两个网站,网站建设设计设计公司哪家好,个人购物网站,邯郸资讯8.7 多态 什么是多态#xff1f; 即同一方法可以根据发送对象的不同而采用多种不同的方式。 一个对象的实际类型是确定的#xff0c;但可以指向对象的引用的类型有很多。在句话我是这样理解的#xff1a; 在实例中使用方法都是根据他最开始将类实例化最左边的类型来定的 即同一方法可以根据发送对象的不同而采用多种不同的方式。 一个对象的实际类型是确定的但可以指向对象的引用的类型有很多。在句话我是这样理解的 在实例中使用方法都是根据他最开始将类实例化最左边的类型来定的但是父类引用指向对象子类的话如果子类有重写的方法即方法名与父类相同的方法但是内容不一定相同那么执行的子类所重写的方法如果没有重写的方法就执行父类的。在这句话中所说的“但可以指向对象的引用的类型有很多”意思是一个父类可以有很多的子类都可以使用他们的重写方法。
示例
父类Person
package com.oop.demo06;public class Person {public void run(){System.out.println(run);}}子类Student
package com.oop.demo06;public class Student extends Person {Overridepublic void run() {System.out.println(son);}public void eat(){System.out.println(eat);}
}测试类 import com.oop.demo06.Student;
import com.oop.demo06.Person;public class AppLication{public static void main(String[] args) {// 一个对象的实际类型是确定的// 可以指向的引用类型就不确定了:父类的引用指向子类// Student 能调用的方法都是自己的或者继承父类的Student S1 new Student();// Person是父类型的类可以指向子类但是不能调用子类独有的方法Person S2 new Student();Object S3 new Student();S2.run(); // 子类重写了父类的方法执行子类的方法S1.run();// 对象能执行能执行哪些方法主要看对象的左边类型和右边关系不大((Student) S2).eat(); //将S2的类型强制转换为Student就可以调用方法了S1.eat();}
}/*多态注意事项1. 多态是方法的多态属性没有多态2. 父类和子类要有联系 类型转换异常 ClassCastException3. 存在条件 继承关系方法需要重写父类引用指向子类对象1. static 方法属于类它不属于实例2. final 常量3. private 方法*/ 8.8 instanceof 和 类型转换 instanceof 使用的方法格式 【实例的名称 instanceof 类的类型名称】判断一个对象是什么类型就是判读两个类型的对象是否有父子关系有的话就返回Ture没有就是返回False。 使用 【实例的名称 instanceof 类的类型名称】时能否编译看的是实例化对象的类型名输出的结果看的是实例化对象时最右边的所实例的对象值。
首先父类是Person其余的类Student、Teacher是Person的子类。
import com.oop.demo06.Student;
import com.oop.demo06.Person;
import com.oop.demo06.Teacher;public class AppLication{public static void main(String[] args) {// Object Person Student// Object Person Tercher// Object StringObject object new Student();System.out.println(object instanceof Student); // trueSystem.out.println(object instanceof Person); // trueSystem.out.println(object instanceof Object); // trueSystem.out.println(object instanceof Teacher); // falseSystem.out.println(object instanceof String); // falseSystem.out.println();Person person new Student();System.out.println(person instanceof Student); // trueSystem.out.println(person instanceof Person); // trueSystem.out.println(person instanceof Object); // trueSystem.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); // trueSystem.out.println(student instanceof Person); // trueSystem.out.println(student instanceof Object); // true
// System.out.println(student instanceof Teacher); // 编译时就报错
// System.out.println(student instanceof String); // 编译时就报错}
} 类型转换关于对象的类型
import com.oop.demo06.Student;
import com.oop.demo06.Person;
import com.oop.demo06.Teacher;public class AppLication{public static void main(String[] args) {// 类型之间的转化 父 子// 高 低Person student new Student();// student 将这个对象转换为Student的对象类型// 我们就可以使用这个对象类型中的方法了((Student) student).go(); // 这个是高转低需要强制转换// 如果是低转高子类转换为父类不需要强制转换可能会丢失一些方法Student student1 new Student();Person person student1;}
} 8.9 static 关键字详解 static修饰符有着静态的意思使用他代表着这个方法或者元素在类中是与类一起加载的。
package com.oop.demo07;// static
public class Student {private static int age; // 静态变量 多线程private double score; // 非静态变量public static void main(String[] args) {Student s1 new Student();System.out.println(s1.score);System.out.println(age);}
}在类中还有着代码块这种东西他的优先级比这个构造器还高。
package com.oop.demo07;public class Person {// {
// // 代码块匿名代码块
// }
//
// static {
// // 静态代码块
// }// 第二个执行可以用来赋初始值{System.out.println(匿名代码块);}// 第一个执行与类一起加载只执行一次不管你实例化多少个对象static {System.out.println(静态代码块);}// 第三个执行构造器public Person(){System.out.println(构造方法);}public static void main(String[] args) {Person person new Person();System.out.println();Person person1 new Person();}
}在Java中有许许多多的有用又有趣的类在这些类中有着许多有用的静态方法可以使用下面这个方法直接调用想查看类的调用位置直接ctrl左键就行。
package com.oop.demo07;// 导入其他包中的类的静态方法
import static java.lang.Math.random; // 返回一个随机数(0,1)
import static java.lang.Math.PI; // 圆周率
public class Test {public static void main(String[] args) {System.out.println(random());System.out.println(PI);}
}需要注意的是 如果给这些东西使用了常量修饰符final那么他将会是唯一的无无法拥有子类。 8.10 抽象类 什么是抽象类abstract 修饰符可以用来修饰方法也可以修饰类如果修饰方法那么该方法就是抽象方法如果修饰类那么就是抽象类。 抽象类中可以没有抽象方法但是有抽象方法的类一定要声明为抽象类。 抽象类不能用new关键字来创建对象它是用来让子类继承的。 抽象方法只有方法的声明没有方法的实现它是用来让子类实现的。 子类继承了抽象类那么他就必须要实现抽象类没有实现的抽象方法否则该子类也要声明为抽象类。
抽象父类
package com.oop.demo08;//abstract 抽象类类
public abstract class Action {// 约束~有人帮我们实现~// 这个就是抽象的方法我们只定义了方法名字他的具体功能由别人实现public abstract void doSomething();// 1. 不能new这个抽象类只能靠子类去实现它约束// 2. 抽象类中可以写普通的方法// 3. 抽象方法必须在抽象类中// 抽象的抽象约束~
}抽象父类的子类他里面有重写父类的方法
package com.oop.demo08;// 抽象类的所有方法继承了他的子类都必须要实现他的方法
public class A extends Action {Overridepublic void doSomething() {}
}8.11 接口的定义与实现 接口的作用1. 约束2. 定义一些方法让不同的人实现3. public abstract4. public static final5. 接口不能实例化接口中没有构造方法6. implements可以实现多个接口7. 必须要重写接口中的方法
接口UserService
package com.oop.demo09;//interface 定义接口的修饰符关键字接口都需要有实现类
public interface UserService {// 接口中定义就是常量 public static finalint AGE 9999;// 接口中的所有定义的方法其实都是抽象的 public abstractvoid add(String name);void delete(String name);void update(String name);void query(String name);
}接口TimeService
package com.oop.demo09;public interface TimeService {void timer();
}每个接口都需要有实现类。我是这样理解的每个接口就是定义一堆方法的名字与类型就是没有里面的内容而他们需要有实现类来实现里面的内容需要用到关键字implements
package com.oop.demo09;// 类 可以实现接口 implements 接口
// 实现了接口的类就需要重新接口中的方法// 利用接口实现多继承
public class UersServiceImpl implements UserService,TimeService{Overridepublic void add(String name) {}Overridepublic void delete(String name) {}Overridepublic void update(String name) {}Overridepublic void query(String name) {}Overridepublic void timer() {}
}8.12 N种内部类 package com.oop.demo10;public class Outer {private int id 14203;public void out(){System.out.println(这是外部类的方法);}public class Inner{public void in(){System.out.println(这是内部类的方法);}// 内部类能够获得外部类的私有属性public void getID(){System.out.println(id);}}
}
import com.oop.demo10.Outer;import java.util.Optional;public class AppLication{public static void main(String[] args) {Outer outer new Outer();// 通过这个外部类来实例化内部类Outer.Inner inner outer.new Inner();inner.in();inner.getID();}
} 局部内部类
package com.oop.demo10;public class Outer {// 局部内部类在方法里的属性也叫做局部变量public void method(){class Inner{}}
}匿名内部类
package com.oop.demo10;public class Test {public static void main(String[] args) {// 没有名字初始化类匿名类// 不用将实例保存在对象中new Apple();new Apple().eat();// 甚至可以匿名一个接口new Userservice(){Overridepublic void happle() {}};}
}class Apple{public void eat(){System.out.println(eat);}
}interface Userservice{void happle();
}