java上机实验(三个) 继承

(1)
1.设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。
2设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected,类型的成员变量r、获取和设置r值的public方法、计算圆面积的一public方法。
3设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public方法。
4.建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。
要求:每个类包含无参数和有参数的构造方法。构造方法用于对成员变量初始化,无参数的构造方法将成员变量初始化为0值。子类的构造方法调用父类的构造方法,对父类中的成员变量初始化。

package 实验六继承;
class point
{
    
    
	protected double x1;//protected类型的成员变量x
	protected double y1;
	
	public void set1(double x,double y){
    
       //设置x和y值的public方法
		this.x1=x;           
		this.y1=y;
	}
	
	public double getx() //获取x
	{
    
    
		return x1;
	}
	
	public double gety() //获取y
	{
    
    
		return y1;
	}

}

class circle extends point{
    
      //继承
	protected double r1;
	
	public void set2(double r)
	{
    
    
		this.r1=r;
	}
	
	public double getr()
	{
    
    
		return r1;
	}
	
	public double getS()
	{
    
    
		return 3.14*r1*r1;
	}
}

class cylinder extends circle
{
    
    
	protected double h1;
	
	public void set3(double h)
	{
    
    
		h1=h;
	}
	
	public double geth()
	{
    
    
		return h1;
	}
	
	public double getV()
	{
    
    
		return getS()*h1;
	}
}
public class point0circle0cylinder {
    
    
	 public static void main(String[] args) 
	 {
    
    
	 cylinder cld=new cylinder(); 
	 cld.set1(2.2, 2.6);  //调用继承的方法
	 cld.set2(1.5);
	 cld.set3(1.5);
	 System.out.println("轴心坐标为"+"("+cld.x1+","+cld.y1+")");
	 System.out.println("半径为"+cld.r1+","+"底面积为"+cld.getS());
	 System.out.println("高为"+cld.h1+"体积为"+cld.getV());
	 }
	 
}

(2)
1.定义一个抽象类Printable,其中包括一个方法printItMyWay(),该方法是一个抽象方法,没有形参,返回值为空。
2.改写矩形类使之继承Printable类,printItMyWay()方法将矩形的相关信息打印在屏幕上,例如矩形的长为多少,矩形的宽为多少。.注:矩形类包含矩形的长和宽。
3.在Printable类中增加一个新的抽象方法printItMyWay(char),这方法有一个字符性参数,返回值为空。其功能是利用给出的字符打印在矩形行区域内在main方法中添加语句调用printItMyWay()和printItMyWay(char)

package 实验六继承;
abstract class Printable   //抽象类
{
    
    
	abstract void printItMyWay();//抽象方法	
	abstract void printItMyWay(char c);
}
class 矩形 extends Printable{
    
    
	
	int x;
	int y;
	char cc;
	public void get(int x1,int y1)
	{
    
    
		x=x1;
		y=y1;
	}
	public void printItMyWay()
	{
    
       
		System.out.println("x="+x+" y="+y);
	}
	public void printItMyWay(Printable c)
	{
    
       
		for(int i=1;i<=x;i++)
		{
    
    
			for(int j=1;j<=y;j++)
			{
    
    
				System.out.print(cc);
			}
			System.out.println();
		}
	}
}
public class print {
    
    

	public static void main(String[] args) {
    
    
		Printable pri=new 矩形();//pri使上转型对象
		矩形  ju=new 矩形();
		ju.get(10, 5);
		ju.cc='#';
		ju.printItMyWay();
		ju.printItMyWay(pri);
		
	}
}

(3)
编写一个java应用程序,除了主类之外,该程序中还有4个类:People, ChinaPeople, AmericanPeople.和BeijingPeople类。要求如下:
●People类有访问权限是protected的double类型成员变量height和weight,以及public void speakHello(),public void averageHeight()和pubic void averageWeight()方法。
●ChinaPeople类是People的子类,新增了public void chinaGongfu方法。要求ChinaPeople重写父类的public void speakHello(),public void averageHeight()和pubic void averageWeight()方法。
●AmericaPeople类是People类的子类,新增publicvoidamericanBoxing()方法。要求AmericaPeople类重写父类的public void speakHello(),public void averageHeight()和pubic void averageWeight()方法。
●BeijingPeople类是ChinaPeople类的子类,新增public void beijingOpera()方法。要求BeijingPeople类重写父类的public void speakHello(),public void averageHeight()和pubic void averageWeight()方法。

package 实验六继承;

class People {
    
    
	   protected double weight,height;
	   
	    public void speakHello( ) {
    
    
	       System.out.println("yayawawa");
	    }  
	    
	    public void averageHeight() {
    
     
	       height=173;
	   System.out.println("average height:"+height);
	    }
	    
	   public void averageWeight( ) {
    
    
	       weight=70;
	       System.out.println("average weight:"+weight);
	    }
	   
	}
	class ChinaPeople extends People {
    
      
		public void speakHello( ) {
    
    
		       System.out.println("你好,吃饭了吗");
		    }   // 重写public void speakHello( )方法,要求输出类似“你好,吃饭了吗” 
	          // 这样的汉语信息
		
		 public void averageHeight() {
    
     
		       height=168.78;
		   System.out.println("ChinaPeople average height:"+height);
		    } // 重写public void averageHeight( )方法,要求输出类似
	          // “中国人的平均身高:168.78厘米”这样的汉语信息
		 
		 public void averageWeight( ) {
    
    
		       weight=65;
		       System.out.println("ChinaPeople average weight:"+weight);
		    }// 重写public void averageWeight( )方法,要求输出类似
	          //“中国人的平均体重:65公斤”这样的汉语信息
		 
	   public void chinaGongfu () {
    
    
	     System.out.println("坐如钟,站如松,睡如弓");// 输出中国武术的信息,如“坐如钟,站如松,睡如弓”等
	   }
	   
	}
	class AmericanPeople  extends People {
    
    
		 
	    public void speakHello( ) {
    
    
	       System.out.println("How do you do");
	    }  
	    
	    public void averageHeight() {
    
     
	       height=175;
	   System.out.println("AmericanPeople average height:"+height);
	    }
	    
	   public void averageWeight( ) {
    
    
	       weight=80;
	       System.out.println("AmericanPeople average weight:"+weight);
	    }
	   public void americanBoxing() {
    
    
		   System.out.println("直拳,钩拳"); // 输出拳击的信息,如“直拳”、“钩拳”等
	   }
	}
	class BeijingPeople extends ChinaPeople {
    
    
		 
	    public void speakHello( ) {
    
    
	       System.out.println("您好");
	    }  
	    
	    public void averageHeight() {
    
     
	       height=173.1321;
	   System.out.println("BeijingPeople average height:"+height);
	    }
	    
	   public void averageWeight( ) {
    
    
	       weight=69;
	       System.out.println("BeijingPeople average weight:"+weight);
	    }
	  public void beijingOpera() {
    
    
		  System.out.println("京剧");   // 输出京剧的信息
	   }
	}
	public class Example {
    
    
	    public static void main(String args[ ]) {
    
    
	      ChinaPeople chinaPeople=new ChinaPeople( );
	      AmericanPeople americanPeople=new AmericanPeople( );
	      BeijingPeople beijingPeople=new BeijingPeople( );
	      chinaPeople.speakHello( );
	      americanPeople.speakHello( );
	      beijingPeople.speakHello( );
	      chinaPeople.averageHeight( );
	      americanPeople.averageHeight( );
	      beijingPeople.averageHeight( );
	      chinaPeople.averageWeight( );
	      americanPeople.averageWeight( );
	      beijingPeople.averageWeight( );
	      chinaPeople.chinaGongfu( );
	      americanPeople.americanBoxing( );
	      beijingPeople.beijingOpera( ) ;
	      beijingPeople.chinaGongfu( );
	    }  
	}

猜你喜欢

转载自blog.csdn.net/weixin_52879528/article/details/124179986