父类
子类
测试类
obj.method输出,子类方法
obj.methodFu输出,父类特有方法
父类
public class Fu {
public void fu(){
System.out.println("父类方法");
}
public void methodFu(){
System.out.println("父类特有方法");
}
}
子类
public class Zi extends Fu {
@Override
public void fu() {
System.out.println("子类方法");
}
public void methodZi(){
System.out.println("父类特有方法");
}
}
测试
public class Test {
public static void main(String[] args) {
Fu obj = new Zi();
obj.fu();
obj.methodFu();
//注意此时的obj找不到子类的methodZi方法
//obj.methodZi //错误写法
}
}
父类
public class Fu {
int num = 10;
public void showNum(){
System.out.println(num);
}
}
子类
public class Zi extends Fu{
int num =20;
@Override
public void showNum() {
System.out.println(num);
}
}
测试
public class Test{
public static void main(String[] args) {
Fu obj = new Zi();
System.out.println(obj.num); //Fu:10
System.out.println("===========");
obj.showNum(); //Zi:20
}
}
多态抽象类
抽象类手机卡
public abstract class SimAbs {
abstract String giveNumber();
abstract String giveCorpName();
}
联通卡
public class LianTong extends SimAbs{
String num ;
String name;
public LianTong(String num, String name) {
this.num = num;
this.name = name;
}
@Override
String giveNumber() {
return num;
}
@Override
String giveCorpName() {
return name;
}
}
电信卡
public class DianXin extends SimAbs{
String num ;
String name;
public DianXin(String num, String name) {
this.num = num;
this.name = name;
}
@Override
String giveNumber() {
return num;
}
@Override
String giveCorpName() {
return name;
}
}
移动卡
public class YiDong extends SimAbs{
String num ;
String name;
public YiDong(String num, String name) {
this.num = num;
this.name = name;
}
@Override
String giveNumber() {
return num;
}
@Override
String giveCorpName() {
return name;
}
}
手机类使用手机卡类
public class Phone {
// SimAbs a=new LianTong("17366668888","中国联通");
SimAbs a;
public Phone(SimAbs a) {
this.a = a;
}
public void show(){
System.out.println("手机号码"+a.giveNumber()+";手机卡类型:"+a.giveCorpName());
}
}
手机测试类 //看卡号码以及卡类型
public class Phone_test {
public static void main(String[] args) {
SimAbs a = new LianTong("17266668888","中国联通");
Phone phone_LianTong = new Phone(a);
phone_LianTong.show();
SimAbs b = new YiDong("13866668888","中国移动");
Phone phone_Yidong = new Phone(b);
phone_Yidong.show();
SimAbs c = new DianXin("17666668888","中国电信");
Phone phone_Dianxin = new Phone(c);
phone_Dianxin.show();
}
}
抽象动物类
public abstract class AnimalAbs {
public abstract String cry(); //叫
public abstract String getAnimalName(); //名字
}
猫
public class Cat extends AnimalAbs{
String call;
String name;
public Cat(String call, String name) {
this.call = call;
this.name = name;
}
@Override
public String cry() {
return call;
}
@Override
public String getAnimalName() {
return name;
}
}
狗
public class Dog extends AnimalAbs{
String call;
String name;
public Dog(String call, String name) {
this.call = call;
this.name = name;
}
@Override
public String cry() {
return call;
}
@Override
public String getAnimalName() {
return name;
}
}
动物叫声模拟器类
public class Simuator {
AnimalAbs a ;
public Simuator(AnimalAbs a) {
this.a = a;
}
public void playsound (){
System.out.println("动物"+a.getAnimalName()+",叫声"+a.cry());
}
}
动物叫声模拟器测试类
public class Simuator_test {
public static void main(String[] args) {
AnimalAbs a = new Dog("汪汪汪", "二哈");
Simuator simuator_dog = new Simuator(a);
simuator_dog.playsound();
AnimalAbs b = new Cat("喵喵喵","kiti");
Simuator simuator_cat = new Simuator(b);
simuator_cat.playsound();
}
}
向上转型及向下转型还原
抽象类
public abstract class AnimalAbs {
public abstract void eat();
}
猫类
public class Cat extends AnimalAbs{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public void zhuaMouse(){
System.out.println("猫抓老鼠");
}
}
测试类
public class Test {
public static void main(String[] args) {
AnimalAbs animal = new Cat();
animal.eat(); // 猫吃鱼
// animal.zhuaMouse(); //错误写法
System.out.println("========");
Cat cat = (Cat)animal;
cat.eat(); //猫吃鱼
cat.zhuaMouse(); //猫抓老鼠
}
}