zoukankan      html  css  js  c++  java
  • Java 练习(继承性练习)

    练习1

    ManKind.java

    package com.klvchen.exer;
    
    public class ManKind {
    	
    	private int sex;     //性别
    	private int salary;  //薪资
    	
    	public ManKind() {
    		
    	}
    
    	public ManKind(int sex, int salary) {
    		this.sex = sex;
    		this.salary = salary;
    	}
    
    	public void manOrWoman() {
    		if(sex == 1) {
    			System.out.println("man");
    		}else if(sex == 0) {
    			System.out.println("woman");
    		}
    	}
    	
    	public void employeed() {
    		String jobInfo = (salary == 0)? "no job": "job";
    		System.out.println(jobInfo);
    	}
    
    	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;
    	}
    
    }
    
    

    Kids.java

    package com.klvchen.exer;
    
    public class Kids extends ManKind{
    	private int yearsOld;
    	
    	public Kids() {
    		
    	}
    	
    	public Kids(int yearsOld) {
    		this.yearsOld = yearsOld;
    	}
    
    	public void printAge() {
    		System.out.println("I am " + yearsOld + " years old.");
    	}
    
    	public int getYearsOld() {
    		return yearsOld;
    	}
    
    	public void setYearsOld(int yearsOld) {
    		this.yearsOld = yearsOld;
    	}
    
    }
    

    KidsTest.java

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

    运行结果:

    练习2

    Circle.java

    package com.klvchen.exer1;
    
    public class Circle {
    	
    	private double radius;   //半径
    	
    	public Circle() {
    		radius = 1.0;
    	}
    
    	public double getRadius() {
    		return radius;
    	}
    
    	public void setRadius(double radius) {
    		this.radius = radius;
    	}
    
    	//返回圆的面积
    	public double findArea() {
    		return Math.PI * radius * radius;
    	}
    }
    

    Cylinder.java

    package com.klvchen.exer1;
    
    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 findArea() * getLength();
    	}
    }
    

    CylinderTest.java

    package com.klvchen.exer1;
    
    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);
    		
    		double area = cy.findArea();
    		System.out.println("底面圆的面积: " + area);
    	}
    
    }
    

    运行结果:

  • 相关阅读:
    发现对各类项目有用的不同JavaScript的Web UI
    PowerDesigner 15.1 安装步骤详细图解及破解
    数据库设计工具PowerDesigner基础普及
    Vistual Studio 2010(VS2010)安装 MVC3.0具体方法
    pb的网络资源【转】
    powerbuider11 C/S 转换为B/S
    转:将可执行文件注册成系统windows服务
    WCF绑定类型选择(转)
    (转)找增强方法总结
    ALV简单模板1
  • 原文地址:https://www.cnblogs.com/klvchen/p/14411871.html
Copyright © 2011-2022 走看看