java继承与多态
Posted Pistachiout
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java继承与多态相关的知识,希望对你有一定的参考价值。
1、设计一个数据类型判断类Polymorphism,使用重载、装箱等技术判断一个不带等号的Java表达式的结果的数据类型。
public class Polymorphism {
private final static String INT_TYPE = "int";
private final static String LONG_TYPE = "long";
private final static String DOUBLE_TYPE = "double";
private final static String FLOAT_TYPE = "float";
private final static String CHAR_TYPE = "char";
private final static String BYTE_TYPE = "byte";
private final static String SHORT_TYPE = "short";
private final static String BOOLAEN_TYPE = "boolean";
private final static String String_TYPE = "String";
public static String getType(int i) {
return INT_TYPE;
}
public static String getType(long l) {
return LONG_TYPE;
}
public static String getType(double d) {
return DOUBLE_TYPE;
}
public static String getType(float f) {
return FLOAT_TYPE;
}
public static String getType(char c) {
return CHAR_TYPE;
}
public static String getType(byte by) {
return BYTE_TYPE;
}
public static String getType(short s) {
return SHORT_TYPE;
}
public static String getType(boolean bo) {
return BOOLAEN_TYPE;
}
public static String getType(String Str) {
return String_TYPE;
}
public static String getType(Object obj){
return obj != null ? obj.toString().split("@")[0] : null;
}
public static void main(String[] args)
{ System.out.println("100是"+getType(100)+"类型");
System.out.println("1ds是"+getType("1ds")+"类型");
System.out.println("8==9是"+getType(8==9)+"类型");
System.out.println("100.15是"+getType(100.15)+"类型");
System.out.println("a是"+getType('a')+"类型");
System.out.println("100+222.5是"+getType(100+222.5)+"类型");
System.out.println("100+'a'是"+getType(100+'a')+"类型");
System.out.println("5.9/54是"+getType(5.9/54)+"类型");
System.out.println("599999999L是"+getType(599999999L)+"类型");
}
}
2、设计一个链表结点类LinkNode
此类可以存放int、long、float、double、byte、short、String、StringBuffer类型的数据。用此类:a、随机产生100个整数(范围自定)的链表,在生成的过程中从小到大排列,然后输出;b、随机产生100个6个英文字母的单词的链表,在生成的过程中从小到大排列,然后输出。(关注装箱和拆箱相关概念 Integer String)
public class LinkNode {
public Object value;
public LinkNode nextNode;
public LinkNode(Object value,LinkNode nextNode)
{this.value=value;
this.nextNode=nextNode;}
public LinkNode()
{}
public LinkNode(Object value)
{this.value=value;}
}
package text5;
public class RandomInt{
public static void main(String[] args) {
StringBuffer StrB=new StringBuffer();
LinkNode firstNode=new LinkNode(0);
firstNode.nextNode=new LinkNode(0);
for(int i=0;i<105;i++)
{ LinkNode aNew=new LinkNode();
LinkNode compare=firstNode.nextNode;
LinkNode before=firstNode;
compare=firstNode.nextNode;
int aNum=(int)(Math.random()*1001);
aNew=new LinkNode(aNum);
if(i==0)
{ compare=aNew;
continue;
}
while(aNum>Integer.parseInt(compare.value.toString()))
{if(compare.nextNode!=null)
{compare=compare.nextNode;
before=before.nextNode;
}
else
{before=before.nextNode;
break;
}
}
if(before!=compare)
{before.nextNode=aNew;
aNew.nextNode=compare;}
else
{
compare.nextNode=aNew;
}
}
for(int i=1;i<103;i++)
{ if(i>2)
{System.out.print(i-2+": "+firstNode.value+" ");}
firstNode=firstNode.nextNode;
if((i-2)%10==0)
System.out.println();
}
}
}package text5;
public class RandomStr {
public static void main(String[] args) {
StringBuffer StrB=new StringBuffer();
LinkNode firstNode=new LinkNode("first");
firstNode.nextNode=new LinkNode(" ");
for(int i=0;i<105;i++)
{ LinkNode aNew=new LinkNode();
LinkNode compare=firstNode.nextNode;
LinkNode before=firstNode;
compare=firstNode.nextNode;
StrB=new StringBuffer();
for (int j=0;j<6;j++)
{int aNum=(int)(Math.random()*26);
char aChar=(char)('a'+aNum);
StrB.append(aChar);
}
aNew=new LinkNode(StrB);
if(i==0)
{
compare=aNew;
continue;
}
while(StrB.toString().compareTo(compare.value.toString())>0)
{if(compare.nextNode!=null)
{compare=compare.nextNode;
before=before.nextNode;
}
else
{before=before.nextNode;
break;
}
}
if(before!=compare)
{before.nextNode=aNew;
aNew.nextNode=compare;}
else
{
compare.nextNode=aNew;
}
}
for(int i=1;i<103;i++)
{ if(i>2)
{System.out.print(i-2+": "+firstNode.value+" ");}
firstNode=firstNode.nextNode;
if((i-2)%10==0)
System.out.println();
}
}
}
3、A、在main()中使用上题的LinkNode类创建4个实例
并赋予不同的值(long、double、 StringBuffer、MyDate),然后使用Object中默认的toString()方法(从超级父类继承而来) 显示结果。
public class LinkNodes extends LinkNode{
public LinkNodes(Object value,LinkNode nextNode)
{this.value=value;
this.nextNode=nextNode;}
public LinkNodes()
{}
public LinkNodes(Object value)
{this.value=value;}
public String toString(long longNum)
{ String str=longNum+"";
return str;
}
public String toString(double doubleNum)
{String str=doubleNum+"";
return str;
}
public String toString(StringBuffer StrBuffer)
{String str=StrBuffer+"";
return str;
}
public String toString(MyDate myDateNum)
{String str=myDateNum.year+" "+myDateNum.month+" "+myDateNum.day+" "+myDateNum.hour+" "+myDateNum.minute+" "+myDateNum.second;
return str;
}
}
B、继承LinkNode类创建新类LinkNodeS
在其中重写Object中默认的toString()方法(将结点的value转换成对应的字符串),main()中用LinkNodeS类同样创建4个实例,并赋予和上面同样的值(long、double、StringBuffer、MyDate),观察使用新的toString()方法的效果,体会继承与多态。(MyDate的日期toString用标准格式)
public class UseOfLinkNodes {
public static void main(String[] args) {
StringBuffer strBuffer=new StringBuffer("aaa");
MyDate myDate=new MyDate();
LinkNode longNum=new LinkNode(594785L);
LinkNode doubleNum=new LinkNode(599.5);
LinkNode StringBuffer=new LinkNode(strBuffer);
LinkNode myDateNum=new LinkNode(myDate);
System.out.println(longNum.value.toString());
System.out.println(doubleNum.value.toString());
System.out.println(StringBuffer.value.toString());
System.out.println(myDateNum.value.toString());
System.out.println();
LinkNodes longNum2=new LinkNodes(594785L);
LinkNodes doubleNum2=new LinkNodes(599.5);
LinkNodes StringBuffer2=new LinkNodes(strBuffer);
LinkNodes myDateNum2=new LinkNodes(myDate);
String myDateNum2Str=myDateNum2.toString((MyDate)myDateNum2.value);
System.out.println(longNum2.toString((long)longNum2.value));
System.out.println(doubleNum2.toString((double)doubleNum2.value));
System.out.println(StringBuffer2.toString((StringBuffer)StringBuffer2.value));
System.out.println(myDateNum2Str);
}
}
4、:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类作为Animal的子类设计类
有来自4个类别(鸟类、昆虫类、爬行类和鱼类)的100个动物聚在一起开会,商议和另一个动物部落打仗事宜,会议要求每个动物都要报告自己所属的动物类别和自己的天赋,以便选拔人才、组织兵力出战。
设计:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类各作为Animal的子类设计类层次结构,设计时运用继承、重写并设计多态机制,同时对每个子类至少要添加一个其描述的动物特有的行为和一个特有的属性,以更准确地描述子类对象。
使用:用循环随机生成这100个动物装入动物数组,要对每个动物进行编号和随机命名,用循环让每个参会的动物报告自己的类别和天赋。
public class Animal {
public int id; //编号
public StringBuffer name;//名字
public String type; //类别:鸟类、昆虫类、爬行类和鱼类之一
public void showType()//在console上秀自己的类别
{
System.out.println("My ID is “+id+” and I am just an animal, my type is going to be determined later.");
}
public void showTalent()//在console上秀自己的天赋特长
{
System.out.println("I don’t know what I am talented at right now.");
}
public void showName()
{
System.out.println("My name is "+this.name);
}
}
class Fish extends Animal
{ public Fish(int id,StringBuffer name)
{this.id=id;
this.name=name;
this.type="fish";
}
public void showType()//在console上秀自己的类别
{
System.out.println("My ID is "+id+" and I am just an animal, my type is fish");
}
public void showTalent()//在console上秀自己的天赋特长
{
System.out.println("I am talented at swiming");
}
}
class Bird extends Animal
{ public Bird(int id,StringBuffer name)
{this.id=id;
this.name=name;
this.type="bird";
}
public void showType()//在console上秀自己的类别
{
System.out.println("My ID is "+id+" and I am just an animal, my type is bird");
}
public void showTalent()//在console上秀自己的天赋特长
{
System.out.println("I am talented at flying");
}
}
class Insect extends Animal
{ public Insect(int id,StringBuffer以上是关于java继承与多态的主要内容,如果未能解决你的问题,请参考以下文章