总结与体会:
今天继续Java的类与对象。自己设计类、接口、抽象类,当自己设置好了还是蛮有成就感。
Debug的使用还是挺方便理解程序的运行过程。
习题
1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。
package com.jiale.day07;
public interface Person {
void setDate(String name, String sex, String birthday);
String getDate();
}
package com.jiale.day07;
public class Student implements Person {
private String name;
private String sex;
private String birthday;
private String sID;
private String speciality;
public Student(String name, String sex, String birthday, String sID, String speciality) {
// TODO自动生成的构造函数存根
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.sID = sID;
this.speciality = speciality;
}
public Student(){}
public String getsID() {
return sID;
}
public void setsID(String sID) {
this.sID = sID;
}
public String getSpeciality() {
return speciality;
}
public void setSpeciality(String speciality) {
this.speciality = speciality;
}
@Override
public void setDate(String name, String sex, String birthday) {
// TODO自动生成的方法存根
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
@Override
public String getDate() {
// TODO自动生成的方法存根
String date = "姓名:"+name+"\n性别:"+sex+"\n生日:"+birthday+"\n学生ID:"+sID+"\n特长:"+speciality;
return date;
}
}
package com.jiale.day07;
public class demo01 {
public static void main(String[] args) {
// TODO自动生成的方法存根
Student stu1 = new Student();
stu1.setDate("张三", "男", "1月1日");
stu1.setsID("001");
stu1.setSpeciality("编程");
System.out.println(stu1.getDate()+"\n");
Student stu2 = new Student("李四", "男", "2月2日", "002", "数学");
System.out.println(stu2.getDate());
}
}
输出结果:
姓名:张三
性别:男
生日:1月1日
学生ID:001
特长:编程
姓名:李四
性别:男
生日:2月2日
学生ID:002
特长:数学
2、编写程序,求柱体的体积:
(1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();
(2)、为柱体设计类pillar,要求:
a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。
b)实现构造方法,对成员变量赋值。
c)包含成员方法,计算柱体pillar的体积。
(3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积。
package com.jiale.day07;
public interface Geometry {
double getArea();
}
package com.jiale.day07;
public class Circle implements Geometry {
private double radius;
public Circle() {}
public Circle(double radius) {
// TODO自动生成的构造函数存根
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
// TODO自动生成的方法存根
return 3.14*radius*radius;
}
}
package com.jiale.day07;
public class Rect implements Geometry {
private double length;
private double wide;
public Rect() {}
public Rect(double length, double wide) {
// TODO自动生成的构造函数存根
this.length = length;
this.wide = wide;
}
public double getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public double getWide() {
return wide;
}
public void setWide(int wide) {
this.wide = wide;
}
@Override
public double getArea() {
// TODO自动生成的方法存根
return length*wide;
}
}
package com.jiale.day07;
public class Pillar {
private double bottomArea;
private double high;
public Pillar() {}
public Pillar(double bottomArea, double high) {
// TODO自动生成的构造函数存根
this.bottomArea = bottomArea;
this.high = high;
}
public double getBottomArea() {
return bottomArea;
}
public void setBottomArea(double bottomArea) {
this.bottomArea = bottomArea;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public void getPillarVolume(){
System.out.println("此柱体的体积为:"+bottomArea*high);
}
}
package com.jiale.day07;
public class dome02 {
public static void main(String[] args) {
// TODO自动生成的方法存根
Circle cir = new Circle(3.0);
Rect rec = new Rect(1.5, 2.1);
Pillar pil1 = new Pillar(cir.getArea(), 5.0);
Pillar pil2 = new Pillar(rec.getArea(), 5.0);
pil1.getPillarVolume();
pil2.getPillarVolume();
}
}
输出结果:
此柱体的体积为:141.29999999999998
此柱体的体积为:15.750000000000002
3设计一个系统:
xxx纯净水生产线
目前流程是:从某个地方把水取出来,然后经过缓冲,过滤,加热和放糖的步骤
abstract 水{
public void 水();
}
interface 过滤{}
interface 缓冲{}
interface 加热{}
interface 放糖{}
class 纯净水1 extends 水 imps 过滤,缓冲{}
class 纯净水2 extends 水 imps 缓冲{}
class 纯净水2 extends 水 imps 过滤{}
package com.jiale.day03;
public abstract class Water {
public abstract void Water();
}
package com.jiale.day03;
public interface Buffer {
public abstract void Buffer();
}
package com.jiale.day03;
public interface Filter {
public abstract void Filter();
}
package com.jiale.day03;
public interface Heat {
public abstract void Heat();
}
package com.jiale.day03;
public interface Sugar {
public abstract void Sugar();
}
package com.jiale.day03;
public class Water1 extends Water implements Filter, Buffer {
@Override
public void Buffer() {
// TODO自动生成的方法存根
System.out.println("水已经缓冲\n");
}
@Override
public void Filter() {
// TODO自动生成的方法存根
System.out.println("水已经过滤\n");
}
@Override
public void Water() {
// TODO自动生成的方法存根
System.out.println("水1的制造过程:\n");
}
}
package com.jiale.day03;
public class Water2 extends Water implements Buffer {
@Override
public void Buffer() {
// TODO自动生成的方法存根
System.out.println("水已经缓冲\n");
}
@Override
public void Water() {
// TODO自动生成的方法存根
System.out.println("水2的制造过程:\n");
}
}
package com.jiale.day03;
public class Water3 extends Water implements Filter {
@Override
public void Filter() {
// TODO自动生成的方法存根
System.out.println("水已经过滤\n");
}
@Override
public void Water() {
// TODO自动生成的方法存根
System.out.println("水3的制造过程:\n");
}
}
运算结果:
水1的制造过程:
水已经缓冲
水已经过滤
水2的制造过程:
水已经缓冲
水3的制造过程:
水已经过滤