1. 多态
多态:同样的类型变量,调用同样的方法,却产生完全不同的行为。
- 当父类类型的变量指向子类的对象,调用父类中已经被重写的方法时,产生多态机制。
- 在多态的情况下,不能调用子类新增的方法,使用强制的类型转化。
- 只有在有父子关系的情况下才能进行强制类型转换。
public class Person {
public String name;
int age;
public String getInfo(){
return "name:" + this.name + ",age:" + this.age;
}
void walk(){
System.out.println("人在走路····");
}
public Person(){
}
public Person(String name, int age){
super();
this.name = name;
this.age = age;
}
}
public class Man extends Person{
void work(){
System.out.println("男人工作!!");
}
void walk(){
System.out.println("男人快速的走路···");
}
}
public class Woman extends Person{
void shopping(){
System.out.println("女人逛街。。。。") ;
}
void walk(){
System.out.println("女人优雅的走路···");
}
}
public class TestPerson {
public static void main(String[] args) {
Person p1 = new Person();
p1.walk();
Man man = new Man();
man.walk();
man.work();
// 一种类型 多种状态
Person p2 = new Man();
p2.walk();
Man m = (Man)p2;
m.work();
//p2.work();
// 多种状态
Person p3 = new Woman();
p3.walk();
}
}
/*
人在走路····
男人快速的走路···
男人工作!!
男人快速的走路···
女人优雅的走路···
*/
2. instanceof
操作符
x instanceof A
检验x是否为A的对象,返回值为boolean
。要求x所属的类与A类必须是子类或者父类的关系,否则编译出错。
3. 一个几何形体的例子
定义一个类GeometricObject,里边有color 和 weight属性。
定义Circle类,继承GeometricObject,增加radius属性。
定义MyRectangle类,继承GeometricObject类,增加 width 和 height 属性。
在测试类中编写equalsArea方法 测试两个对象的面积是否相等。
3.1 代码1GeometricObject
public abstract class GeometricObject {
protected String color; // protected 可以被子类访问
protected double weight;
// ctrl + / 是注释多行
// ctrl + shift + / 是 注释代码块
// protected GeometricObject(String color, double weight){
// this.color = color;
// this.weight = weight;
// }
public GeometricObject(String color, double weight) {
this.color = color;
this.weight = weight;
}
// 自动生成 get 和 set 方法
// 右键 generator
// 或者是 alt + insert 快捷键
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
// 多态的重要性
public double findArea(){ // 抽象的概念 没办法去求面积
// 定义这样的代码 意义尤为重大
return 0;
}
}
3.2 代码2 Circle
public class Circle extends GeometricObject{
private double radius;
public Circle(String color, double weight, double radius) {
super(color, weight);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){ // 方法的重写
return Math.PI * this.radius * this.radius;
}
}
3.3 代码3MyRectangle
public class MyRectangle extends GeometricObject{
private double width;
private double heigth;
public MyRectangle(String color, double weight, double width, double heigth) {
super(color, weight);
this.width = width;
this.heigth = heigth;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeigth() {
return heigth;
}
public void setHeigth(double heigth) {
this.heigth = heigth;
}
public double findArea(){ // 方法的重写
return this.heigth * this.width;
}
}
3.4 代码4TestGeometricObject
public class TestGeometricObject {
public boolean equalsArea(GeometricObject o1, GeometricObject o2){
return o1.findArea() == o2.findArea();
}
public void displayGeometricObject(GeometricObject o){
System.out.println("area:" + o.findArea()); // sout 快捷生成输出语句
}
public static void main(String[] args) {
TestGeometricObject tgo = new TestGeometricObject();
Circle c1 = new Circle("", 0, 2);
Circle c2 = new Circle("", 0, 3);
boolean ret = tgo.equalsArea(c1, c2);
System.out.println("c1和c2的面积是否相等:" + ret);
tgo.displayGeometricObject(c1);
tgo.displayGeometricObject(c2);
MyRectangle m1 = new MyRectangle("", 0, 2, 6.28);
boolean ret2 = tgo.equalsArea(c1, m1);
System.out.println("ret2 = " + ret2);;
}
}
/*
c1和c2的面积是否相等:false
area:12.566370614359172
area:28.274333882308138
ret2 = false
*/