华恩JAVA班第25天

华恩JAVA班第25天

 

 

 

今天对抽象类,对象转型,多态,匿名对象做了一个总结

 


abstract:是一个修饰符 可以修饰方法,类
1.当我们多个类存在相同的功能,但功能的主体不同,
这时进行向上抽取,只抽取功能的定义不抽取功能的主体

抽象类的特点:
1.抽象方法一定要在抽象类中
2.抽象类和抽象方法必须要被abstract关键字修饰
3.抽象的类是不能被创建对象,因为调用抽象方法没意义
4.抽象类中的方法要被使用,必须由子类重写抽象类中的方法,
然后创建子类对象来调用
5.抽象类中可以定义非抽象的方法,有时我们需要此类不能
被new关键字创建对象时,可以用abstract将此类变成抽象类
6.子类如果只重写一部分的抽象方法,那么该子类还是一个抽象类
如果抽象类的方法要被使用,子类必须重写抽象抽象类中德所有方法

注意:抽象类与普通类没有太大不同
1.抽象类无法通过new关键字创建对象
2.抽象类里面可以有抽象的方法

abstract class Animal{
 abstract void sing();
 void show(){
  System.out.println("你好");
 }
}
class Cat extends Animal{
 void sing(){
  System.out.println("喵喵唱歌");
 }
}
class Dog extends Animal{
 void sing(){
  System.out.println("汪汪唱歌");
 }
}
public class Test{
 public static void main(String[] args) {
  Animal a = new Cat();
  a.sing();
 }
}


abstract class Vehicle{
 abstract String NoOfWheels();
}
class Car extends Vehicle{
  String NoOfWheels(){
  return "四轮车";
 }
}
class Motorbike extends Vehicle{
 String NoOfWheels(){
  return "双轮车";
 }
}
public class Test1{
 public static void main(String[] args) {
  Vehicle a = new Car();
  print(a);
  Vehicle b = newMotorbike();
  print(b);
 }
 public static void print(Vehicle a){
  System.out.println(a.NoOfWheels());
 }
}

 

多态:可以理解为事物存在的多种体现形态

1.多态的体现
父类引用指向子类对象
2.多态的前提
(1)必须得类与类之间存在关系,可以是继承关系,也可以是实现关系
(2)必须得有重写
3.多态的好处
大大提高了程序的课扩展性
4.多态的弊端
只能用父类的引用,访问到父类中的成员

多态成员变量的特点:
1.编译期间:参阅的是引用型变量所属的类中是否有调用的方法
2.运行期间:参阅对象所属类中是否有调用的方法

class Animal{
 void sing(){
  System.out.println("唱歌");
 }
}
class Dog extends Animal{
 void sing(){
  System.out.println("汪汪的唱歌");
 }
}
class Cat extends Animal{
 void sing(){
  System.out.println("喵喵的唱歌");
 }
 void catchMouse(){
  System.out.println("捕鼠");
 }
}
public class Test{
 public static void main(String[] args) {
  Cat c = new Cat();
  function(c);
 }
 public static void function(Animal a){
  a.sing();

  Cat c = (Cat)a;
  c.catchMouse();
 }
}

 

class Pet{
 void cry(){
 }
 void eat(){
 }
}
class Dog extends Pet{
 void cry(){
  System.out.println("汪汪的哭");
 }
 void eat(){
  System.out.println("狗吃了骨头");
 }
 void guardEntrance(){
  System.out.println("狗在看门");
 }
}
class Cat extends Pet{
 void cry(){
  System.out.println("喵喵的哭");
 }
 void eat(){
  System.out.println("猫吃了鱼");
 }
 void huntMice(){
  System.out.println("猫在捕鼠");
 }
}
public class Test2{
 public static void main(String[] args) {
  Pet pet1 = new Dog();
  Pet pet2 = new Cat();
  print(pet1);
  Dog d = (Dog)pet1;
  d.guardEntrance();
  print(pet2);
  Cat c = (Cat)pet2;
  c.huntMice();
 }
 public static void print(Pet pet){
   pet.cry();
   pet.eat();
  }
}

 

匿名对象:没有名字的对象
匿名对象的使用方式之一:当对对象的方法只调用一次时,简化代码
匿名对象的使用方式之二:匿名对象可以被当做实参传递

class Car{
 String color;
 void start(){
  System.out.println("汽车在开动");
 }
}
public class Test{
 public static void main(String[] args) {
  //new Car().color ="blue";  没意义
  //new Car().start();

  //Car c = new Car();
  //print(c);
  print(new Car());
 }
 static void print(Car c){
  c.start();
 }
}

 

对象的转型:
1.对象的向上转型 子类转成父类 默认进行
2.对象的向下转型 父类转成子类 强制进行

关键字:instanceof 测试左边的对象是否是右边类的实例
如果是返回true 不是返回false

class Animal{
 void sleep(){
  System.out.println("睡觉中");
 }
}
class Cat extends Animal{
 void catchMouse(){
  System.out.println("捕鼠");
 }
}
class Dog extends Animal{
}
public class Test{
 public static void main(String[] args) {
  Animal a = newCat();//向上转型
  a.sleep();
  Cat c = (Cat)a;//向下转型
  c.sleep();
  c.catchMouse();
  System.out.println(a instanceofAnimal);
  System.out.println(a instanceofCat);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值