笔记
多态
public class Uncle {
private String name ;
private int age ;
public Uncle(){
}
public void fahongbao(){
System.out.println("发红包");
}
子类一
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 14:57
*/
public class UncleTwo extends Uncle{
public void faHongbao(){
System.out.println("发红包");
}
public void songyan(){
System.out.println("送烟");
}
}
子类二
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 14:56
*/
public class UncleOne extends Uncle{
public void faHongbao(){
System.out.println("发红包");
}
}
多态
UncleOne dajiu = new UncleOne();
dajiu.faHongbao(); //大舅发红包
UncleTwo uncleTwo = new UncleTwo() ;
uncleTwo.faHongbao(); //二舅发红包
向上转型
Uncle dajiu1 = new UncleOne() ;
dajiu1.fahongbao();
向下转型
Uncle dajiu1 = new UncleOne() ;
dajiu1.fahongbao();
//子类独有的方法在发生向上转型的时候无法在父类中使用
//dajiu1.songyan //会报错
UncleOne temp = (UncleOne) dajiu1 ; //向下转型
temp.songyan(); //可以调用子类独有的方法
instanceof
判断对象是否是指定的类型实例
避免发生错误的类型转换
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 15:58
*/
public class Demo02 {
public static void main(String[] args) {
Uncle uncle1= new UncleOne() ;
Uncle uncle2 = new UncleTwo() ;
if (uncle1 instanceof UncleOne){
UncleOne u1 = (UncleOne) uncle1 ;
u1.fahongbao();
}
if (uncle2 instanceof UncleTwo){
UncleTwo u2 = (UncleTwo) uncle2 ;
u2.faHongbao();
}
}
}
Demo01
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 14:45
*/
public class Demo01 {
public static void main(String[] args) {
//多态
UncleOne dajiu = new UncleOne();
dajiu.faHongbao();
UncleTwo uncleTwo = new UncleTwo() ;
uncleTwo.faHongbao();
Uncle dajiu1 = new UncleOne() ;
dajiu1.fahongbao();
//dajiu1.songyan //会报错 子类独有的方法在发生向上转型的时候无法在父类中使用
UncleOne temp = (UncleOne) dajiu1 ;
temp.songyan();
Uncle erjiu = new UncleTwo() ;
erjiu.fahongbao();
//向下转型
Uncle uncleTwo1 = new UncleTwo() ;
UncleTwo temp1 = (UncleTwo) uncleTwo;
temp.faHongbao();
}
}
Demo02
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 15:58
*/
public class Demo02 {
public static void main(String[] args) {
Uncle uncle1= new UncleOne() ;
Uncle uncle2 = new UncleTwo() ;
if (uncle1 instanceof UncleOne){
UncleOne u1 = (UncleOne) uncle1 ;
u1.fahongbao();
}
if (uncle2 instanceof UncleTwo){
UncleTwo u2 = (UncleTwo) uncle2 ;
u2.faHongbao();
}
}
}
Uncle
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 14:47
*/
public class Uncle {
private String name ;
private int age ;
public Uncle(){
}
public Uncle(String name , int age){
this.name = name ;
this.age = age ;
}
public void fahongbao(){
System.out.println("发红包");
}
public String getName(){
return name ;
}
@Override
public String toString() {
return "Uncle{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
UncleOne
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 14:56
*/
public class UncleOne extends Uncle{
public void faHongbao(){
System.out.println("发红包");
}
public void songyan(){
System.out.println("送烟");
}
}
UncleTwo
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/25 14:57
*/
public class UncleTwo extends Uncle{
public void faHongbao(){
System.out.println("发红包");
}
}