到底什么是多态??

twtetgso 2003-03-19 10:19:56
在JAVA中看到多态,不太明白意思,哪位能用通俗的语句或例子给讲一下,谢谢!
...全文
585 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
oldDonkey 2003-03-19
  • 打赏
  • 举报
回复
看来还是有很多人把重载跟多态混淆。
bunny198212 2003-03-19
  • 打赏
  • 举报
回复
public class MultiConstructor
{

//成员变量
int variable1;
int variable2;
int variable3;

//构造方法1
private MultiConstructor()
{
}
//构造方法2
private MultiConstructor(int variable1)
{
this.variable1 = variable1;
}
//构造方法3
private MultiConstructor(int variable1,int variable2)
{
this.variable1 = variable1;
this.variable2 = variable2;
}
//构造方法4
private MultiConstructor(int variable1,int variable2,int variable3)
{
this.variable1 = variable1;
this.variable2 = variable2;
this.variable3 = variable3;
}

}
hoxisoft 2003-03-19
  • 打赏
  • 举报
回复
:)
bunny198212 2003-03-19
  • 打赏
  • 举报
回复
多态是指一个程序中同名的不同方法共存的情况,面向对象的程序中多态的情况有多种,可以通过子类对父类方法的覆盖实现多态,也可以用重载在同一个类中定义多个同名的不同方法。
多态情况下进行方法调用时只需在调用方法时指定是调用的是哪个类的方法就可以区分它们。下面附例子!!!
snoopydotnet 2003-03-19
  • 打赏
  • 举报
回复
//: c12:Shapes.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import java.util.*;

class Shape {
void draw() {
System.out.println(this + ".draw()");
}
}

class Circle extends Shape {
public String toString() { return "Circle"; }
}

class Square extends Shape {
public String toString() { return "Square"; }
}

class Triangle extends Shape {
public String toString() { return "Triangle"; }
}

public class Shapes {
public static void main(String[] args) {
ArrayList s = new ArrayList();
s.add(new Circle());
s.add(new Square());
s.add(new Triangle());
Iterator e = s.iterator();
while(e.hasNext())
((Shape)e.next()).draw();
}
} ///:~
这是《Thinking in Java》里的一个例程,通常多态总是跟继承、向上转型、动态绑定等分不开,弄清楚这些概念后就很容易理解。祝你成功!
网络咖啡 2003-03-19
  • 打赏
  • 举报
回复
楼上错了,你说的是方法重载。

多态就是多种形态,利于类型匹配,因为Java是强类型语言,而有些时候类型不一定能够完全匹配,所以就产生了多态。
Kick_hotdog 2003-03-19
  • 打赏
  • 举报
回复
public void setValue(String value){}
public void setValue(float value){}
public void setValue(int value){}
方法名相同,参数不同。
xxlcg 2003-03-19
  • 打赏
  • 举报
回复
我不认为他们把重载跟多态混淆了
shine333 2003-03-19
  • 打赏
  • 举报
回复
就是你对同一支部队(同一个父类)的不同士兵(不同子类)下同样的命令(方法),他们会按自己的方式干不同的事

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧