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 name) 
		{this.id=id;
		this.name=name;
		this.type="insect";
		}

		public void showType()//在console上秀自己的类别
		{
			System.out.println("My ID is "+id+" and I am just an animal, my type is insect");
		}
		public void showTalent()//在console上秀自己的天赋特长
		{
			System.out.println("I am talented at working");
		}
		
	}
	class Reptile extends Animal
	{	public Reptile(int id,StringBuffer name) 
		{this.id=id;
		this.name=name;
		this.type="reptile";
		}

		public void showType()//在console上秀自己的类别
		{
			System.out.println("My ID is "+id+" and I am just an animal, my type is reptile");
		}
		public void showTalent()//在console上秀自己的天赋特长
		{
			System.out.println("I am talented at crawling");
		}
	}

public class AnimalRandom {
	public static void main(String[] args) {
		Animal[] animalArmy = new Animal[100]; 
		for(int i = 0; i<100; i++)
		{
			int type = (int)( Math.random()*4 );
			StringBuffer name = new StringBuffer();
			for(int j = 0; j<4; j++)
			{
				int word = (int)( Math.random()*26 );
				name.append( (char)(word + 'a') );
			}
			switch (type)
			{
			case 0:
				animalArmy[i] = new Bird(i, name);
				break;
			case 1:
				animalArmy[i] = new Fish(i, name);
				break;
			case 2:
				animalArmy[i] = new Insect(i, name);
				break;
			case 3:
				animalArmy[i] = new Reptile(i,name);
			}
			animalArmy[i].showName();
			animalArmy[i].showType();
			animalArmy[i].showTalent();
			System.out.println();				
}
}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pistachiout

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值