Java++开发实战经典第六章课后习题答案.doc_第1页
Java++开发实战经典第六章课后习题答案.doc_第2页
Java++开发实战经典第六章课后习题答案.doc_第3页
Java++开发实战经典第六章课后习题答案.doc_第4页
Java++开发实战经典第六章课后习题答案.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

interface ClassName public String getClassName() ;class Company implements ClassNameprivate String className ; public Company()public Company(String className)this.className = className ;public String getClassName()return this.className ;public class ExecDemo01public static void main(String args)ClassName cn = new Company(Company类) ;System.out.println(cn.getClassName() ;abstract class Shapepublic abstract float area() ;class Triangle extends Shapeprivate float foot ;private float height ;public Triangle()public Triangle(float foot,float height)this.setFoot(foot) ;this.setHeight(height) ;public float area()return this.foot * height / 2 ;public void setFoot(float foot)this.foot = foot ;public void setHeight(float height)this.height = height ;public float getFoot()return this.foot ;public float getHeight()return this.height ;class Cycle extends Shapeprivate float radius ;private static final float PI = 3.1415926f ;public Cycle()public Cycle(float radius)this.setRadius(radius) ;public float area()return PI * this.radius * this.radius ;public void setRadius(float radius)this.radius = radius ;public float getRadius()return this.radius ;public class ExecDemo02public static void main(String args)Shape s1 = new Triangle(30.0f,50.6f) ;Shape s2 = new Cycle(25.3f) ;System.out.println(s1.area() ;System.out.println(s2.area() ;abstract class Personprivate String name ;private String addr ;private char sex ;private int age ;public Person()public Person(String name,String addr)this(name,addr,男,0) ;public Person(String name,String addr,char sex,int age)this.setName(name) ;this.setAddr(addr) ;this.setSex(sex) ;this.setAge(age) ;public abstract String getInfo() ;public void setName(String name) = name ;public void setAddr(String addr)this.addr = addr ;public void setSex(char sex)this.sex = sex ;public void setAge(int age)this.age = age ;public String getName()return ;public String getAddr()return this.addr ;public char getSex()return this.sex ;public int getAge()return this.age ;class Student extends Personprivate 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.setMath(math) ;this.setEnglish(english) ;public String getInfo()return学生信息: + n + t|- 姓名: + super.getName() + n + t|- 年龄: + super.getAge() + n + t|- 地址: + super.getAddr() + n + t|- 性别: + super.getSex() + n + t|- 数学成绩: + this.getMath() + n + t|- 英语成绩: + this.getEnglish() ;public void setMath(float math)this.math = math ;public void setEnglish(float english)this.english = english ;public float getMath()return this.math ;public float getEnglish()return this.english ;public class ExecDemo03public static void main(String args)Person per = new Student(张三,北极,男,30,90.0f,90.0f) ;System.out.println(per.getInfo() ;- - - - - abstract class Employee- private String name ;- private char sex ;- private int age ;- public Employee()- public Employee(String name,char sex,int age)- this.setName(name) ;- this.setSex(sex) ;- this.setAge(age) ;- - public abstract String getInfo() ;- public void setName(String name)- = name ;- - public void setSex(char sex)- this.sex = sex ;- - public void setAge(int age)- this.age = age ;- - public String getName()- return ;- - public char getSex()- return this.sex ;- - public int getAge()- return this.age ;- - - class Manager extends Employee- private String job ;- private float income ;- public Manager()- public Manager(String name,char sex,int age,String job,float income)- super(name,sex,age) ;- this.setJob(job) ;- this.setIncome(income) ;- - public void setJob(String job)- this.job = job ;- - public void setIncome(float income)- this.income = income ;- - public String getJob()- return this.job ;- - public float getIncome()- return this.income ;- - public String getInfo()- return管理层信息: + n + - t|- 姓名: + super.getName() + n + - t|- 年龄: + super.getAge() + n + - t|- 性别: + super.getSex() + n + - t|- 职位: + this.getJob() + n + - t|- 年薪: + this.getIncome() ;- - - class Worker extends Employee- private String dept ;- private float salary ;- public Worker()- public Worker(String name,char sex,int age,String dept,float salary)- super(name,sex,age) ;- this.setDept(dept) ;- this.setSalary(salary) ;- - public String getInfo()- return员工信息: + n + - t|- 姓名: + super.getName() + n + - t|- 年龄: + super.getAge() + n + - t|- 性别: + super.getSex() + n + - t|- 部门: + this.getDept() + n + - t|- 月薪: + this.getSalary() ;- - public void setDept(String dept)- this.dept = dept ;- - public void setSalary(float salary)- this.salary = salary ;- - public String getDept()- return this.dept ;- - public float getSalary()- return this.salary ;- - - public class ExecDemo04- public static void main(String args)- Employee m = new Manager(张三,M,30,技术主管,90.0f) ;- Employee w = new Worker(李四,F,19,后勤,900.0f) ;- System.out.println(m.getInfo() ;- System.out.println(w.getInfo() ;- - - - abstract class Shape- public abstract float area() ;- public abstract float perimeter() ;- - class Triangle extends Shape- private float foot ;- private float height ;- public Triangle()- public Triangle(float foot,float height)- this.setFoot(foot) ;- this.setHeight(height) ;- - public float area()- return this.foot * height / 2 ;- - public float perimeter()- return foot * 3 ;- - public void setFoot(float foot)- this.foot = foot ;- - public void setHeight(float height)- this.height = height ;- - public float getFoot()- return this.foot ;- - public float getHeight()- return this.height ;- - - class Cycle extends Shape- private float radius ;- private static final float PI = 3.1415926f ;- public Cycle()- public Cycle(float radius)- this.setRadius(radius) ;- - public float area()- return PI * this.radius * this.radius ;- - public void setRadius(float radius)- this.radius = radius ;- - public float getRadius()- return this.radius ;- - public float perimeter()- return PI * 2 * this.radius ;- - - public class ExecDemo05- public static void main(String args)- Shape s1 = new Triangle(30.0f,50.6f) ;- Shape s2 = new Cycle(25.3f) ;- System.out.println(s1.area() ;- System.out.println(s1.perimeter() ;- System.out.println(s2.area() ;- System.out.println(s2.perimeter() ;- - - - interface Goods/ 商品- public float getPrice() ;- public String getName() ;- - class ShopCar/ 购物车- private Goods goods ;/ 保存商品- private int foot ; - public ShopCar(int len)- if(len0)- this.goods = new Goodslen ;- else- this.goods = new Goods1 ;/ 至少保持一个大小- - - public void add(Goods goods)/ 向里面增加商品- if(this.footthis.goods.length)- this.goodsthis.foot+ = goods ;/ 添加商品- - - public float check()- float count = 0.0f ;- for(int x=0;xthis.goods.length;x+)- if(this.goodsx!=null)- count += this.goodsx.getPrice() ;- - - return cou

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论