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);
    	}
    
    }
    

    运行结果:

  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/klvchen/p/14411871.html
Copyright © 2011-2022 走看看