-
多态举例
2013-12-22 19:41:08/* 基础班学生 学习,睡觉。 高级班学生: 学习,睡觉。 可以将这两类事物进行抽取。 */ abstract class Student { public abstract void study(); public void sleep() ...public/*
基础班学生
学习,睡觉。
高级班学生:
学习,睡觉。
可以将这两类事物进行抽取。
*/
abstract class Student
{
public abstract void study();
public void sleep()
{
System.out.println("躺着睡");
}
}
/*
public void doSome(BaseStudent bs)
{
}
public void doSome(AdvStudent bs)
{
}
*/
/*
public void doSome(Student stu)//体现了多态性
{
stu.study();
stu.sleep();
}
*/
class DoStudent
{
public void doSome(Student stu)//体现了多态性
{
stu.study();
stu.sleep();
}
}
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 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();
}
} -
java多态举例_java多态实例
2021-02-27 11:11:09public class printerDemo {public static void main(String[] args) {colorPrinter cp = new colorPrinter("惠普");blackPrinter bp = new blackPrinter("联想");school sch = new school();sch.setColorPrinter(cp...public class printerDemo {
public static void main(String[] args) {
colorPrinter cp = new colorPrinter("惠普");
blackPrinter bp = new blackPrinter("联想");
school sch = new school();
sch.setColorPrinter(cp);
sch.setBlacPkrinter(bp);
cp.print("hello");
bp.print("hello");
}
}
class printer{
private String brand;
public String getBrand() {
return brand;
}
public printer(String brand){
this.brand = brand;
}
public void print(String content) {//需要重写
System.out.println(brand);
}
}
class school{
private colorPrinter cp = null;
private blackPrinter bp = null;
public void setColorPrinter(colorPrinter cp) {//安装彩色打印机
this.cp = cp;
}
public void setBlacPkrinter(blackPrinter bp) {//安装黑白打印机
this.bp = bp;
}
public void print(String content) {
cp.print(content);
bp.print(content);
}
}
class colorPrinter extends printer{
public colorPrinter(String brand){
super(brand);
}
public void print(String content) {//子类重写父类方法
System.out.println(getBrand()+"彩色打印:"+content);
}
}
class blackPrinter extends printer{
public blackPrinter(String brand){
super(brand);
}
public void print(String content) {//子类重写父类方法
System.out.println(getBrand()+"黑白打印:"+content);
}
}
-
Java中多态举例说明
2019-07-15 20:19:57public class 多态 { public static void main(String[] args) { Animal a = new cat (); a.show(); Animal b = new dog(); b.show(); f(a); f(b); } public static void f(Animal animal) { if...这里我也就大概说一下他们的关系,
接口就是动物,然而每一个类就是一种动物
给猫有两个功能:叫和睡觉
狗:叫
在f方法里面可以把猫的功能实现
但不能实现狗的功能
在主方法里面有一个猫有一个狗
分别调用
这里一个类可以继承于多个接口下面
继承的多个接口用,分开
再举个例子就是
鳄鱼和蚂蚁都能爬,但是鳄鱼还能进水,而蚂蚁不行
鳄鱼可以继承陆地和水
而蚂蚁只能继承陆地
interface Animal{//接口 void show(); } class cat implements Animal{//猫是动物的一种,就继承在他下面 public void show() { System.out.println("喵喵喵……"); } public void sleep() { System.out.println("睡觉……"); } } class dog implements Animal{//狗同上 public void show() { System.out.println("汪汪汪……"); } } public class 多态 { public static void main(String[] args) { Animal a = new cat (); a.show(); Animal b = new dog(); b.show(); f(a); f(b); } public static void f(Animal animal) { if(animal instanceof cat) {//Java自带方法看an是不是cat的子集 //如果不是cat的子集,就跳过,dog中没有睡觉一说 cat cat = (cat) animal; cat.show(); cat.sleep(); } else { System.out.println("this animal is not a cat"); } } }
-
Java多态举例
2014-12-12 10:32:05功能:多态(Polymorphism),又叫动态绑定【根据实际类型调用(而非引用类型)】 好处:可扩展性达到最好 */ class Animal { private String name; Animal(String name) { this.name = name; } public .../* 时间:2014年12月7日16:30:46 功能:多态(Polymorphism),又叫动态绑定【根据实际类型调用(而非引用类型)】 好处:可扩展性达到最好 */ class Animal { private String name; Animal(String name) { this.name = name; } public void enjoy() { System.out.println("叫声……"); } } class Cat extends Animal { // private String name; //为啥总爱写这句呢? private String eyeColor; Cat(String n, String c) { super(n); eyeColor = c; } public void enjoy() { System.out.println("猫叫声……"); } } class Dog extends Animal { // private String name; //同 Cat 类 private String furColor; Dog(String n, String c) { super(n); furColor = c; } public void enjoy() { System.out.println("狗叫声……"); } } /* class Bird extends Animal { Bird() { super("bird"); } public void enjoy() { System.out.println("鸟叫声……"); } } */ class Lady { private String name; private Animal pet; //pet 的类型是 Animal类(引用类型) Lady(String name, Animal pet) { this.name = name; this.pet = pet; } public void myPetEnjoy() { pet.enjoy(); } } public class TestPolymorphism { //画内存分配 public static void main(String[] args) { Cat c = new Cat("BigYellow", "yellow"); Dog d = new Dog("Awang", "black"); // Bird b = new Bird(); Lady l1 = new Lady("L1", c); //根据实际类型调用(而非引用类型) Lady l2 = new Lady("L2", d); // Lady l3 = new Lady("L3", b); l1.myPetEnjoy(); l2.myPetEnjoy(); // l3.myPetEnjoy(); } } /***********************输出结果: 猫叫声…… 狗叫声…… *************************/
-
封装继承多态举例
2017-03-16 21:50:19大家都知道php是面向对象的,面向对象就少不了封装继承多态。 1.什么时候封装函数?1.内容重复利用2.单独功能,不同的功能可以封装成不同的函数其实每个功能都可以封装成一个函数,封装好了这样方便在主函数中调用,... -
关于python类的多态 举例理解
2020-08-09 17:26:45类的多态特性,要满足以下 2 个前提条件:继承:多态一定是发生在子类和父类之间;重写:子类重写了父类的方法。 class WhoSay: def say(self,who): who.say() class CLanguage: def say(self): print("调用的是... -
继承和多态举例
2016-07-24 09:39:00确切的说,多态是父类使用被子类覆盖的同名方法,如果子类的方法是全新的,父类不存在同名的方法,则父类也不能使用子类自己独有的方法。 转载于:https://www.cnblogs.com/Andya/p/5700138.html -
java多态举例
2013-02-25 13:18:52// java多态存在的三个必要条件:1.需要有继承关系的存在 2.需要有方法的重写 3.需要有父类的引用指向子类对象 // 集成后子类包含父类方法;注意重写的方法;如果没有匹配方法,应该类型转换 System.out.... -
java继承和多态举例
2017-08-27 13:08:00public ...多态实现条件:1、继承 2、方法重写 3、父类申明子类对象 多态实现的机制:动态绑定机制。 方法看对象,属性看声明 转载于:https://www.cnblogs.com/CodingAndRiding/p/7440130.html -
证明字段不能多态 举例
2018-04-26 15:16:21//证明java中字段不能多态,通过对象调用字段,再编译时期就已经决定调用那一块内存空间 //class FiledDemo { public static void main(String[] args) { System.out.println(“HELLO”); ... -
java中接口实现多态举例
2017-08-27 20:01:00public class Test4 { public static void main(String[] args){ Instrument ss[]={new Wind(),new Piano()}; for(Instrument i:ss){ tun(i); } ... -
java多态性举例_【豆豆话题】java多态举例解析
2021-02-27 14:14:381. 哪些函数能够调用,取决于reference的类型2. 对于被override 的函数,调用哪个版本,取决于object的类型3. 如果非要调用reference 没有的类型,可以强制类型转换成object的类型class A{public String show(D obj)... -
Javascript中,多态举例
2013-12-25 16:34:13先上代码,再解释:function Base(){ this.x=1; } Base.prototype.m1 = function(){ this.m2(); }; Base.prototype.m2 = function(){ console.log("Base.m2()"); }; //////////////////////////////...function Chi -
重载、覆盖、隐藏、多态举例说明
2018-03-19 13:41:38例子一:class A{ public: A(int m);int setValue(int mval);void setValue();private:int m_data; }分析:该类中, 函数setValue(int mval) 和 函数setValue()为重载成员函数。因为,函数名相同,参数不同,... -
多态
2019-02-21 21:53:42文章目录多态多态定义多态举例代码多态发生的三个必要条件虚析构函数 多态 多态定义 C++中所谓的多态是指:由继承而产生的子类,对同一消息而产生不同的行为 多态举例代码 #include "stdafx.h... -
多态应用举例2
2015-08-25 11:40:39#include class Base{ public: void fun1(){ this->fun2();//this 基类指针 fun2虚函数 所以是多态 } virtual void fun2(){ cout } }; class Derived: -
java接口多态_JAVA利用接口实现多态(举例)
2021-02-12 14:47:26interface CallBack{void callBack(int param);}class Client implements CallBack{public void callBack(int p){System.out.println("Call back called with :"+p);}void nonIterfaceMeth(){System.out.println("Cl... -
多态思想并举例
2016-02-13 23:40:15*多态:可以理解为事物存在的多种体现形态。 *比如人:男人 女人 * 动物:猫 狗 * 猫 x = new 猫(); * 动物 x = new 猫(); *对象的多态性 * *1,多态的基本体现 * 代码体现形式: 父类的引用指定... -
【含答案】第四章 接口与多态--4.4-多态的应用举例
2020-12-10 15:01:37【含答案】第四章 接口与多态--4.4-多态的应用举例 (1)单选题 import java.io.*; class Person{ public void print(){System.out.print("Person ");} public void printMyGender(String s){ this.print(); ... -
什么是多态?请举例
2020-07-18 21:57:25多态是一个引用类型在不同的情况下的多种状态,多态是只通过指向父类的指针,来调用在不同类中实现的方法;父类引用指向子类对象 例子: package com.qt; /** * 演示多态的例子 * @author Administrator * */... -
多态和继承举例
2016-07-28 09:15:24程序1 需求:建立一个人类(Person)和学生类(Student)功能如下...确切的说,多态是父类使用被子类覆盖的同名方法,如果子类的方法是全新的,父类不存在同名的方法,则父类也不能使用子类自己独有的方法。