赞
踩
11.1(三角形Triangle)
父类(左边) 和 子类(右边)继承关系,
如下图:
- package p11;
-
- public class GeometricObject {
- private String color = "white";
- private boolean filled;
- private java.util.Date dateCreated;
-
- public GeometricObject() {
- dateCreated=new java.util.Date();
- }
-
- public GeometricObject(String color, boolean filled) {
- dateCreated=new java.util.Date();
- this.color=color;
- this.filled=filled;
- }
-
- public String getColor() {
- return color;
-
- }
-
- public void setColor(String color) {
- this.color=color;
- }
-
- public boolean isFilled() {
- return filled;
-
- }
-
- public void setFilled(boolean filled) {
- this.filled=filled;
-
- }
-
- public java.util.Date getDateCreated() {
- return dateCreated;
-
- }
-
- public String toString() {
- return "created on " + dateCreated + "\ncolor: " + color+
- " and filled: " + filled;
-
- }
- }
- package p11;
-
- public class Triangle extends GeometricObject {
- private double side1 = 1.0;
- private double side2 = 1.0;
- private double side3 = 1.0;
-
- public Triangle() {
-
- }
-
- //这是一个 重载 的构造方法
- public Triangle(double side1, double side2,
- double side3, String color, boolean filled) {
-
- super(color,filled);//或者 调用父类中已有方法 ,如下
- //setColor(color);
- //setFilled(filled);
- this.side1=side1;
- this.side2=side2;
- this.side3=side3;
-
- }
-
- public double getSide1() {
- return side1;
-
- }
-
- public double getSide2() {
- return side2;
-
- }
-
- public double getSide3() {
- return side3;
-
- }
-
- public double getArea() {
- double s=(side1+side2+side3)/2;
- double area=Math.pow( s*(s-side1)*(s-side2)*(s-side3) , 0.5);
- return area;
- }
-
- public double getPerimeter() {
- return side1+side2+side3;
-
- }
-
- @Override
- public String toString() {
-
- return super.toString() + "\nArea is " + getArea();
-
- }
- }
- package p11;
-
- import java.util.Scanner;
-
- public class Test1 {
-
- public static void main(String[] args) {
- Scanner input=new Scanner(System.in);
- System.out.print("请输入三角形的三条边:");
- double s1=input.nextInt();
- double s2=input.nextInt();
- double s3=input.nextInt();
-
- System.out.print("请输入三角形的颜色: ");
- String c1=input.next();
-
- System.out.print("请输入一个boolean值(false:不填充;true:填充):");
- boolean f1=input.nextBoolean();
-
- input.close();
-
- //创建对象
- GeometricObject object=new Triangle(s1, s2, s3, c1, f1);
-
- System.out.println(object.toString());
-
-
-
- }
-
- }
(11.5~11.14节)
11.2(Person、Student、Employee、Faculty 和 Staff类)
这个不难,我也就是在 受聘日期 那里“卡壳”了一下
总体的继承关系:
- package p11;
-
- public class Person {
- private String name;
- private String adress;
- private String phone;
- private String email;
-
- public Person(String name, String adress, String phone, String email) {
- this.name=name;
- this.adress=adress;
- this.phone=phone;
- this.email=email;
- }
-
- public Person(String name) {
- this.name=name;
- }
-
- public String getAdress() {
- return adress;
-
- }
-
- public void setAdress(String adress) {
- this.adress=adress;
- }
-
- public String getPhone() {
- return phone;
-
- }
-
- public void setPhone(String phone) {
- this.phone=phone;
- }
-
- public String getEmail() {
- return email;
-
- }
-
- public void setEmail(String email) {
- this.email=email;
-
- }
-
- public String toString() {
- return "The Person's name is "+name+"\nAdress is "+adress+
- "\nPhone is "+phone+"\nEmail is "+email;
-
- }
- }
- package p11;
-
- public class Student extends Person {
-
- private int classState;
-
- public final static int FRESHMAN=1;//大一新生
- public final static int SOPHOMORE=2;//大二学生
- public final static int JUNIOR=3;//大三学生
- public final static int SENIOR=4;//大四学生
-
-
- public Student(int classState, String name, String adress, String phone, String email) {
- super(name, adress, phone, email);
- this.classState=classState;
-
- }
-
- public int getClassState() {
- return classState;
-
- }
-
- public void setClassState(int classState) {
- this.classState=classState;
- }
-
- @Override
- public String toString() {
- return "The Student's classState is "+classState+"\n"+super.toString();
-
- }
- }
- package p11;
-
- import java.util.Date;
-
- import p10.MyDate;
-
- public class Employee extends Person {
-
- private String office;
- private double salary;
- private MyDate DateOfAppointment;//受聘日期
-
- //构造方法
- public Employee(String office, double salary, MyDate DateOfAppointment,
- String name, String adress, String phone, String email) {
-
- super(name, adress, phone, email);
-
- this.DateOfAppointment=DateOfAppointment;
- this.salary=salary;
- this.office=office;
-
- }
-
- public String getOffice() {
- return office;
-
- }
-
- public void setOffice(String office) {
- this.office=office;
- }
-
- public double getSalary() {
- return salary;
-
- }
-
- public void setSalary(double salary) {
- this.salary=salary;
- }
-
- public String getDateOfAppointment() {
-
- return DateOfAppointment.getYear()+"年"+DateOfAppointment.getMonth()
- +"月"+DateOfAppointment.getDay()+"日";
-
- }
-
- @Override
- public String toString() {
- return "The Employee's dateOfAppointment is "+ getDateOfAppointment()
- +"\nOffice is "+office+"\nSalary is "+salary+"\n"+super.toString();
-
- }
- }
- package p11;
-
- import p10.MyDate;
-
- public class Faculty extends Employee {
- private long officeHours;
- private String rank;
-
- //构造方法
- public Faculty(long officeHours, String rank, String office, double salary, MyDate DateOfAppointment,
- String name, String adress, String phone, String email) {
-
- super(office, salary, DateOfAppoint
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。