继承性练习

1.

(1)定义ManKind类

public class ManKind {
     private int sex;
     private int salary;
     //constructor
     public ManKind(){
    	 
     }
     public ManKind(int sex,int salary){
    	 this.sex=sex;
    	 this.salary=salary;
     }
     
     //*******method******
     public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public void manOrWoman(){
    	 if(sex==1){
    		 System.out.println("man");
    	 }
    	 if(sex==0){
    		 System.out.println("woman");
    	 }
     }
     public void employeed(){
    	 if(salary==0){
    		 System.out.println("no job");
    	 }
    	 if(salary!=1){
    		 System.out.println("job");
    	 }
     }
}

 (2)定义Kids类继承ManKind

public class Kids extends ManKind {
       private int yearsOld;
    //constructor
       public Kids(){  	   
       }
	public Kids(int yearsOld) {
		this.yearsOld = yearsOld;
	}
    //********method********
	public int getYearsOld() {
		return yearsOld;
	}
	public void setYearsOld(int yearsOld) {
		this.yearsOld = yearsOld;
	}

	public void printAge(){
    	   System.out.println("I am "+yearsOld);
       }
}

(3)定义KidsTest进行测试

public class KidsTest {
       public static void main(String[] args){
    	   Kids someKid=new Kids(12);
    	   someKid.printAge();
    	   someKid.setSex(0);
    	   someKid.setSalary(11111000);
    	   someKid.manOrWoman();
    	   someKid.employeed();
       }
}

2.根据下图实现类。在CylinderTest 类中创建 Cylinder 类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。

(1) 定义Circle类

public class Circle {
      private double radius;
      //constructor
      public Circle(){
    	  radius=1.0;
      }
      public Circle(double radius){
    	  this.radius=radius;
      }
      //*****method******
      public void setRadius(double radius){
    	  this.radius=radius;
      }
      public double getRadius(){
    	  return radius;
      }
      public double findArea(){
    	  return Math.PI*radius*radius;
      }
}

(2) 定义Cylinder类继承Circle类

public class Cylinder extends Circle{
	
	private double length;//高
	
	public Cylinder(){
		length = 1.0;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}
	
	//返回圆柱的体积
	public double findVolume(){
//		return Math.PI * getRadius() * getRadius() * getLength();
		return super.findArea() * getLength();
	}
	
	@Override
	public double findArea() {//返回圆柱的表面积
		return Math.PI * getRadius() * getRadius() * 2 + 
				2 * Math.PI * getRadius() * getLength();
	}

}

​

(3)定义CylinderTest进行测试 

public class CylinderTest {
	public static void main(String[] args) {
		
		Cylinder cy = new Cylinder();
		
		cy.setRadius(2.1);
		cy.setLength(3.4);
		double volume = cy.findVolume();
		System.out.println("圆柱的体积为:" + volume);
		
		//没有重写findArea()时:
//		double area = cy.findArea();
//		System.out.println("底面圆的面积:" + area);
		//重写findArea()以后:
		double area = cy.findArea();
		System.out.println("圆柱的表面积:" + area);
		
		System.out.println("******************");
		Cylinder cy1 = new Cylinder();
		double volume1 = cy1.findVolume();
		System.out.println("圆柱的体积为:" + volume1);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值