封装一个类Person和它的两个子类,封装一个抽象类Shape

第七题:封装一个类Person和它的两个字类:Student和Employee;

主函数代码为

具体的数据请自行修改,请不要完全抄袭

每一个代码段都要创建一个新的类,创建一个新的*.java文件

public class U3T78 {
    public static void main(String[] args) {
        Student student=new Student();
        Staff staff=new Staff();
        Teacher teacher=new Teacher();
        MyDate myDate=new MyDate();
        student.setName("123");
        student.setAddress("烟台");
        student.setPhone("1432");
        student.setEmail("214");
        student.toString();
        staff.setName("qwe");
        staff.setAddress("yt");
        staff.setMyDate("2022", "2", "1");
        staff.setEmail("213");
        staff.setPhone("1312");
        staff.setTitle("保洁");
        staff.setOffice("3");
        staff.setSalary("2000¥");
        staff.toString();
        teacher.setName("213");
        teacher.setAddress("烟台");
        teacher.setPhone("12424");
        teacher.setEmail("1232");
        teacher.setOffice("213");
        teacher.setSalary("9000¥");
        teacher.setMyDate("2021", "21", "1");
        teacher.setTime("1年");
        teacher.setLevel("讲师");
        teacher.toString();

    }
}


public class Person {
    String name;
    String address;
    String phone;
    String email;

    public void setName(String name){
        this.name=name;
    }
    public void setAddress(String address){
        this.address=address;
    }
    public void setPhone(String phone){
        this.phone=phone;
    }
    public void setEmail(String email){
        this.email=email;
    }

    public String toString(){
        System.out.println("姓名:"+name);
        System.out.println("地址:"+address);
        System.out.println("电话:"+phone);
        System.out.println("邮箱:"+email);
        return null;
    }
}

 

public class Student extends Person{
    public final String ID="大二";
    public String toString(){
        System.out.println("学生");
        System.out.println("年纪:"+ID);
        System.out.println("姓名:"+name);
        System.out.println("地址:"+address);
        System.out.println("电话:"+phone);
        System.out.println("邮箱:"+email);

        return null;
    }
}
public class Employee extends Person {
    String office;
    String salary;
    MyDate myDate=new MyDate();

    public void setOffice(String office){
        this.office=office;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }
    public  void setMyDate(String year,String month,String day){
        myDate.year=year;
        myDate.month=month;
        myDate.day=day;
    }

    public String toString(){
        System.out.println("姓名:"+name);
        System.out.println("办公室:"+office);
        System.out.println("工资:"+salary);
        myDate.toString();
        System.out.println("地址:"+address);
        System.out.println("电话:"+phone);
        System.out.println("邮箱:"+email);
        return null;
    }
}
public class Staff extends Employee {
    String title;

    public void setTitle(String title) {
        this.title = title;
    }
    public String toString(){
        System.out.println("职工");
        System.out.println("姓名:"+name);
        System.out.println("办公室:"+office);
        System.out.println("职务:"+title);
        System.out.println("工资:"+salary);
        myDate.toString();
        System.out.println("地址:"+address);
        System.out.println("电话:"+phone);
        System.out.println("邮箱:"+email);
        return null;
    }
}
public class Teacher extends Employee {
    String time;
    String level;

    public void setTime(String time) {
        this.time = time;
    }

    public void setLevel(String level) {
        this.level = level;
    }
    public String toString(){
        System.out.println("教师");
        System.out.println("姓名:"+name);
        System.out.println("办公室:"+office);
        System.out.println("办公时间:"+time);
        System.out.println("职称级别:"+level);
        System.out.println("工资:"+salary);
        myDate.toString();
        System.out.println("地址:"+address);
        System.out.println("电话:"+phone);
        System.out.println("邮箱:"+email);
        return null;
    }
}
public class MyDate{
    String year;
    String month;
    String day;

    public void setYear(String year) {
        this.year = year;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public void setDay(String day) {
        this.day = day;
    }
    public String toString(){
        System.out.println("聘任日期:"+this.year+"年"+this.month+"月"+this.day+"日");
        return null;
    }
}

第八题 

封装一个抽象类Shape,其中包括有求形状面积的抽象方法和球形状周长的非抽象方法……

数据请自行修改

public class U3T78 {
    public static void main(String[] args) {
        Triangle Triangle=new Triangle();
        Rectangle Rectangle=new Rectangle();
        Circle Circle=new Circle();
        Triangle.getArea(2, 2, 2);
        Triangle.getPerimeter(2, 2, 2);
        Rectangle.getArea(4, 6, 1);
        Rectangle.getPerimeter(4, 6);
        Circle.getArea(3.1,24,21);
        Circle.getPerimeter(3.1);
    }
}
public abstract class Shape {
    public abstract void getArea(double length1,double length2,double length3);
    public void getPerimeter(double length){

    }
}
public class Triangle extends Shape{
    public void getArea(double length1,double length2,double length3){
        double p=(length1+length2+length3)/2;
        double area=Math.sqrt(p*(p-length1)*(p-length2)*(p-length3));
        System.out.println("三角形的面积是"+area);
    }
    public void getPerimeter(double length1,double length2,double length3){
        System.out.println("三角形周长是"+(length2+length1+length3));
    }
}

public class Rectangle extends Shape {
    public void getArea(double long1,double width2,double length3) {
        System.out.println("矩形的面积是"+(long1*width2));
    }
    public void getPerimeter(double long1,double width2){
        System.out.println("矩形的周长是"+(long1+width2)*2);
    }
}
public class Circle extends Shape {
    public void getArea(double radius1,double length2,double length3) {
        double area;
        area = radius1 * radius1 * Math.PI;
        System.out.println("圆的面积是"+area);
    }

    public void getPerimeter(double radius){
        double perimeter;
        perimeter=radius*2*Math.PI;
        System.out.println("圆的周长是"+perimeter);
    }

}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值