Java经典实战开发第六章的练习题答案

             Java经典实战开发第六章的练习题答案

一:定义一个ClassName接口,接口中有只有一个抽象方法ClassName;设计一个类Company,实现接口ClassName中的getClassName(),功能是获取该类的名称

public interface ClassName {
	public String getClassName();
}
public class Company implements ClassName {
	private String className;

	public Company() {
	}

	public Company(String className) {
		this.className = className;
	}

	public String getClassName() {
		return this.className;
	}

	public static void main(String[] args) {
		ClassName cn = new Company("Company类");
		System.out.println(cn.getClassName());
	}

}
//结果:Company类

二:一个表示图形的类,写出类中的属性和方法(跟第五题类似,第五题省略)

public abstract class Shape {// 图形类
	public abstract float area();
}
public class Triangle extends Shape {// 三角形的类
	private float length;
	private float wide;

	public Triangle() {
	}

	public Triangle(float length, float wide) {
		super();
		this.length = length;
		this.wide = wide;
	}

	public float getLength() {
		return this.length;
	}

	public void setLength(float length) {
		this.length = length;
	}

	public float getWide() {
		return this.wide;
	}

	public void setWide(float wide) {
		this.wide = wide;
	}

	public float area() {
		return this.length * wide / 2;
	}

	public static void main(String[] args) {
		Shape s1 = new Triangle(20.0f, 30.f);
		System.out.println(s1.area());
	}
}
//结果:300.0

三:建立一个人类和学生类,要求学生类继承人类,并输出关于人类和学生类中的属性

public abstract class Person {
	private String name;
	private String addr;
	private char sex;
	private int age;

	public Person() {
	}

	public Person(String name, String addr) {
		super();
		this.name = name;
		this.addr = addr;
	}

	public Person(String name, String addr, char sex, int age) {
		super();
		this.name = name;
		this.addr = addr;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddr() {
		return this.addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public char getSex() {
		return this.sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return this.age;
	}

	public void setAge(int age) {
		if (age > 0 && age < 150) {
			this.age = age;
		}
	}

	public abstract String getInfo();// 获取4种属性
}
public class Student extends Person {
	private float math;
	private float english;

	public Student() {
	}

	public Student(String name, String addr) {
		super(name, addr);
	}

	public Student(String name, String addr, char sex, int age, float math, float english) {
		super(name, addr, sex, age);
		this.english = english;
		this.math = math;
	}

	public float getMath() {
		return this.math;
	}

	public void setMath(float math) {
		this.math = math;
	}

	public float getEnglish() {
		return this.english;
	}

	public void setEnglish(float english) {
		this.english = english;
	}

	public String getInfo() {
		return "学生名字" + super.getName() + ",学生地址:" + super.getAddr() + ",学生性别:" + super.getSex() + ",学生年龄"
				+ super.getSex() + ",数学成绩:" + this.math + ",英语成绩:" + this.english;
	}

	public static void main(String[] args) {
		Person per = new Student("小白", "潮阳", '男', 18, 99.0f, 100.0f);
		System.out.println(per.getInfo());
	}
}

四:定义员工类,具有姓名,年龄,性别,拥有构造方法金额显示数据的类。定义管理层类,继承员工类,并拥有自己的属性职务和年薪。定义职员类,继承员工类,并有自己的属性职务和月薪。

public abstract class Staff {//员工类
	private String name;
	private int age;
	private char sex;
	public Staff() {}
	public Staff(String name,int age,char sex) {
		super();
		this.name=name;
		this.age=age;
		this.sex=sex;
	}
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return this.age;
	}
	public void setAge(int age) {
		if(age>0&&age<150) {
		this.age = age;
		}
	}
	public char getSex() {
		return this. sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public abstract String getInfo();
}
public class Boss extends Staff {//管理层类
	private String duty;
	private float yearmoney;
	public Boss() {}
	public Boss(String name,int age,char sex,String duty,float yearmoney) {
		super(name,age,sex);
		this.duty=duty;
		this.yearmoney=yearmoney;
	}
	public String getDuty() {
		return this. duty;
	}
	public void setDuty(String duty) {
		this.duty = duty;
	}
	public float getYearmoney() {
		return this.yearmoney;
	}
	public void setYearmoney(float yearmoney) {
		this.yearmoney = yearmoney;
	}
	public String getInfo() {
		return "姓名:"+super.getName()+",年龄:"+super.getAge()+
				"性别:"+super.getSex()+",职务:"+this.duty+",年薪:"+this.yearmoney;
	}
}
public class OfficeClerk extends Staff {//职员类
	private String duty;
	private float mothmoney;
	public OfficeClerk() {
		super();
	}
	public OfficeClerk(String name,int age,char sex,String duty, float mothmoney) {
		super(name,age,sex);
		this.duty = duty;
		this.mothmoney = mothmoney;
	}
	public String getDuty() {
		return this.duty;
	}
	public void setDuty(String duty) {
		this.duty = duty;
	}
	public float getMothmoney() {
		return this. mothmoney;
	}
	public void setMothmoney(float mothmoney) {
		this.mothmoney = mothmoney;
	}
	public String getInfo() {
		return "姓名:"+super.getName()+",年龄:"+super.getAge()+
				"性别:"+super.getSex()+",职务:"+this.duty+",年薪:"+this.mothmoney;
	}
}
public class Test4 {

	public static void main(String[] args) {
		Staff s1=new Boss("张三",30,'男',"经理",100.0f);
		Staff s2=new OfficeClerk("李四",30,'男',"保安",10.0f);
		System.out.println(s1.getInfo());
		System.out.println(s2.getInfo());
		
	}

}
//结果:姓名:张三,年龄:30性别:男,职务:经理,年薪:100.0
姓名:李四,年龄:30性别:男,职务:保安,年薪:10.0

六:用面向对象的概念表示小明去超市买东西,所有买到的东西都放在购物车之中,最后到收银台结账。

public interface Goods {//商品
	public float getPrice();
	public String getName();
}
public class ShopCar {//购物车
	private Goods goods[];//保存商品
	private int foot;
	public ShopCar(int length) {
		if(length>0) {
			this.goods=new Goods[length];
		}else {
			this.goods=new Goods[1];//至少有一件商品
		}
	}
	public void add(Goods goods) {//添加商品
		if(this.foot

 

你可能感兴趣的:(Java练习题)