面向对象—12—多态
来源:互联网 发布:软件图标在线制作 编辑:程序博客网 时间:2024/05/21 18:36
/*
1,多态的体现 Animal c = new Cat();
父类的引用指向了自己的子类对象
父类的引用可以接受自己的子类对象
2,多态的前提
必须是类与类之间有关系。要么继承,要么实现
通常还有一个前提:存在覆盖。
3,多态的好处
多态的出现大大的提高了程序的扩展性
4,多态的弊端
提高了扩展性,但是只能使用父类的引用访问父类中的成员
5,多态的应用
*/
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("吃骨头");
}
public void kanJia()
{
System.out.println("看家");
}
}
class Pig extends Animal
{
public void eat()
{
System.out.println("饲料");
}
public void gongDi()
{
System.out.println("拱地");
}
}
class DuoTaiDemo
{
public static void main(String[] args)
{
//Cat c = new Cat();
//c.eat();
//Dog d = new Dog();
//d.eat();
//Animal c = new Cat();
//c.eat();
function(new Cat());
function(new Dog());
function(new Pig());
}
public static void function(Animal a)//父类的引用指向子类的对象
{
a.eat();
}
/*
public static void function(Cat c)
{
c.eat();
}
public static void function(Dog d)
{
d.eat();
}
public static void function(Pig p)
{
p.eat();
}
*/
}
------------------------------------------------------------------------------------------------------------------
/*
1,多态的体现 Animal c = new Cat();
父类的引用指向了自己的子类对象
父类的引用可以接受自己的子类对象
2,多态的前提
必须是类与类之间有关系。要么继承,要么实现
通常还有一个前提:存在覆盖。
3,多态的好处
多态的出现大大的提高了程序的扩展性
4,多态的弊端
提高了扩展性,但是只能使用父类的引用访问父类中的成员
5,多态的应用
6,多态的出现代码中的特点()
*/
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("吃骨头");
}
public void kanJia()
{
System.out.println("看家");
}
}
class Pig extends Animal
{
public void eat()
{
System.out.println("饲料");
}
public void gongDi()
{
System.out.println("拱地");
}
}
class DuoTaiDemo2
{
public static void main(String[] args)
{
//Animal a = new Cat();//类型提升,向上转型。
//a.eat();
//如果想要调用猫的特有方法时,如何操作?
//强制将父类的引用,转成子类类型。即向下转型
// Cat c = (Cat)a;
// c.catchMouse();
//千万不能出现这样的操作,就是将父类的对象转成子类类型
//我们能转换的是父类引用指向了自己的子类对象时,该引用可以被提上,也可以被强制转换
//多态自始至终都是子类对象在做着变化。
function(new Cat());
function(new Dog());
}
public static void function(Animal a)
{
a.eat();
if(a instanceof Cat)//判断引用类型是否包含
{
Cat c = (Cat)a;
c.catchMouse();
}
else if(a instanceof Dog)
{
Dog d = (Dog)a;
d.kanJia();
}
}
}
--------------------------------------------------------------------------------------------------------------------
/*
1,多态的体现 Animal c = new Cat();
父类的引用指向了自己的子类对象
父类的引用可以接受自己的子类对象
2,多态的前提
必须是类与类之间有关系。要么继承,要么实现
通常还有一个前提:存在覆盖。
3,多态的好处
多态的出现大大的提高了程序的扩展性
4,多态的弊端
提高了扩展性,但是只能使用父类的引用访问父类中的成员
5,多态的应用
6,多态的出现代码中的特点(多态使用的注意事项)
基础班学生:学习,睡觉
高级班学生:学习,睡觉
可以将这两类事物进行抽取
*/
abstract class Student
{
public abstract void study();
public void sleep()
{
System.out.println("躺着睡");
}
}
class BaseStudent extends Student
{
public void study()
{
System.out.println("base study");
}
public void sleep()
{
System.out.println("坐着睡");
}
}
class AdvStudent extends Student
{
public void study()
{
System.out.println("adv study");
}
}
class DoStudent
{
public static void doSome(Student s)
{
s.study();
s.sleep();
}
}
class DuoTaiDemo3
{
public static void main(String[] args)
{
DoStudent ds = new DoStudent();
ds.doSome(new BaseStudent());
ds.doSome(new AdvStudent());
// BaseStudent bs = new BaseStudent();
// bs.study();
// bs.sleep();
//
// AdvStudent as = new AdvStudent();
// as.study();
// as.sleep();
}
}
-----------------------------------------------------------------------------------------------------
个人总结:记住多态的实现前提是:继承或者实现接口。通常还有复习
创建对象时是父类的引用指向自己子类的对象,注意对象类型的向上转型和向下转型
- 面向对象—多态
- 面向对象—12—多态
- 面向对象——多态
- 面向对象——多态
- 面向对象——多态
- 面向对象——多态
- 面向对象——多态
- 面向对象——多态
- OC面向对象—多态
- OC面向对象—多态
- OC面向对象—多态
- OC面向对象—多态
- OC面向对象—多态
- OC面向对象—多态
- 面向对象—
- 面向对象—抽象
- 面向对象—04
- Java—面向对象
- 增加Android输入法键盘语言
- linux 编译指定库、头文件的路径问题
- jquery uploadify 上传文件出现http error404
- birt地址栏传参乱码
- 数据挖掘--购物车 推荐算法
- 面向对象—12—多态
- 文件遍历(枚举 查找)工具
- windows窗口消息处理函数
- 一个电子工程师的经验之谈
- getElementByID getElementsByName getElementsByTagName
- Android中Context详解 ---- 你所不知道的Context
- 一、lucene2.4的创建和查询及其分词显示
- 严重: Exception starting filter struts Action class [com.util.upload] not found - action - file:/C:/Pr
- HDOJ 1038