java简单练习 注:属性没有重写一说,属性要就近原则

文章展示了JAVA编程中的类设计,包括Person类的创建、冒泡排序实现,以及老师类、教授类、副教授类和讲师类的继承关系与工资计算方法。同时,提到了员工类及其子类部门经理和普通员工的工资打印功能,以及房屋出租项目的house类设计。文章强调了静态方法的使用和项目分层模式的概念。
摘要由CSDN通过智能技术生成

JAVA复习Day10 简单练习加简单项目

创建一个Person类,里面有姓名,年龄,工作,然后创建一个含有3个对象的对象数组,按照年龄进行冒泡排序,从大到小

package com.charpter08;
class Person{
    private  int age;
    private String name;
    private  String job;
    public Person(){

    }//无参构造器

    public Person(int age, String name, String job) {
        this.age = age;
        this.name = name;
        this.job = job;
    }//有参构造器

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getJob() {
        return job;
    }
    public String  info(){
        return "姓名:"+name+"\t年龄:"+age+"\t工作:"+job;
    }//输出信息
}
public class main {
    public static void main(String[] args) {
        Person [] person1= new Person[3];
        Person  person2= new Person();//设置一个person2用于暂时存放person的对象
        person1[0]= new Person(67,"小明","歌手");//创建对象
        person1[1]= new Person(20,"小丹","演员");//创建对象
        person1[2]= new Person(23,"小晶","医生");//创建对象
        for (int i=person1.length-1;i>1;i--){
            for (int j=i-1;j>=0;j--){
                if (person1[i].getAge()>person1[j].getAge()){
                    person2=person1[j];//这里就是实际应用
                    person1[j]=person1[i];
                    person1[i]=person2;
                }
            }
        }
        for (int i=0;i<person1.length;i++){
            System.out.println(person1[i].info());
        }//输出信息
    }
}

! 程序运行图

编写老师类,属性包括姓名,年龄,职称,基本工资
编写业务方法,实现输出一个教师的信息
编写教师类的三个子类,教授类,副教授类,讲师类没工资级别为1.3;1.2;1.1
在三个子类中都重写父类的业务方法
定义并初始化一个老师对象,调用业务方法实现对象基本信息的后台打印

在本题中的缺陷就是没有将级别归入到父类中,有待改进,

package com.charpter08.work03;
public class teacher {//父类
        private  String  name;//姓名
        private int age;
        private String post;
        private double salary;//基本工资
        public teacher(String name, int age, String post, double salary) {
            this.name = name;
            this.age = age;
            this.post = post;
            this.salary = salary;
        }//构造器
        public String  info(){
            return "姓名:"+name+"\t年龄:"+age+"\t职称:"+post+"\t工资:"+salary;
        }//业务方法
    public String getName() {
        return name;
    }//由于属性为私有类,所以需要用公有方法去返回
    public int getAge() {
        return age;
    }
    public String getPost() {
        return post;
    }
    public double getSalary() {
        return salary;
    }
}
package com.charpter08.work03;
public class Professor extends  teacher {
    public Professor(String name, int age, String post, double salary) {
        super(name, age, post, salary);
    }//子类继承父类,这里需要进行父类进行构造
    private  double level=1.3;
   public String  info(){
         return "姓名:"+this.getName()+"\t年龄:"+this.getAge()+"\t职称:"+this
                 .getPost()+"\t工资:"+this.getSalary()+"工资级别为:"+level;
    }//输出的时候是本对象的姓名工资等
}
package com.charpter08.work03;
public class preProfessor extends  teacher {
    public preProfessor(String name, int age, String post, double salary) {
        super(name, age, post, salary);
    }//子类继承父类,这里需要进行父类进行构造
    private  double level=1.2;
    public String  info(){
        return "姓名:"+this.getName()+"\t年龄:"+this.getAge()+"\t职称:"+this
                .getPost()+"\t工资:"+this.getSalary()+"工资级别为:"+level;
    }//输出的时候是本对象的姓名工资等
}
package com.charpter08.work03;

public class teacher1 extends teacher {
    public teacher1(String name, int age, String post, double salary) {
        super(name, age, post, salary);
    }
    private  double level=1.1;
    public String  info(){
        return "姓名:"+this.getName()+"\t年龄:"+this.getAge()+"\t职称:"+this
                .getPost()+"\t工资:"+this.getSalary()+"工资级别为:"+level;
    }
}

! 程序运行图

package com.charpter08.work03;
public class works03 {
    public static void main(String[] args) {
        Professor professor = new Professor("老李",37,"教授",55.4);
        System.out.println(professor.info());
        teacher1 teacher11 = new teacher1("老刘",34,"讲师",45.7);
        System.out.println(teacher11.info());
        preProfessor preProfessor1 = new preProfessor("老白",43,"副教授",45.2);
        System.out.println(preProfessor1.info());
    }
}

父类:员工类
子类:部门经理,普通员工
部门经历工资=1000+单日工资×天数×等级
普通员工工资=单日工资×天数×等级
员工属性:姓名。单日工资,工作天数
员工方法(打印工资)
普通员工及部门经理都是员工子类,需要重新打印工资方法
定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象。调用打印工资方法输入工资

package com.charpter08.work04;

public class employee {//父类
    private  String name;
    private  double salary;//每日工资
    private  int day;//工作天数
    private  double grade;//等级类型
    public void println(){
        System.out.println(name+"工资="+salary*day*grade);
    }
    public employee(String name, double salary, int day, double grade) {
        this.name = name;
        this.salary = salary;
        this.day = day;
        this.grade = grade;
    }
    public String getName() {
        return name;
    }
    public double getSalary() {
        return salary;
    }
    public int getDay() {
        return day;
    }
    public double getGrade() {
        return grade;
    }
}
package com.charpter08.work04;
public class manager extends  employee{//子类
    private  double bonus;
    //创建此对象的时候,奖金是不确定的
    public manager(String name, double salary, int day, double grade) {
        super(name, salary, day, grade);
    }
    public void pritln(){
        System.out.println("经理"+getName()+"工资="
        +(bonus+getDay()*getSalary()*getGrade()));
    }
    public double getBonus() {
        return bonus;
    }
    public void setBonus(double bonus) {
        this.bonus = bonus;
    }
}
package com.charpter08.work04;

public class work extends employee{//子类
    public work(String name, double salary, int day, double grade) {
        super(name, salary, day, grade);
    }

    public void println() {//普通员工和父类的输出工资情况一致,所以是一致的
        super.println();
    }
}
package com.charpter08.work04;

public class homework04 {
    public static void main(String[] args) {
        manager manager = new manager("刘备",100,20,1.3);
        manager.setBonus(3000);
        manager.pritln();
        work work = new work("小明",50,10,1.0);
        work.println();
    }
}

! 程序运行图

设计父类-员工类
子类:工人类,农民类,教师类,科学家类,服务生类
其中工人,农民,服务生只有基本工资
教师除基本工资外,还有课酬(元/天)
科学家除基本工资外还有年终奖
编写一个测试类,将各种类型的员工的全年工资打印出来

package com.charpter08.work05;

public class employee {//员工类
    private double salary;//基本工资
    private String name;
    private int month=12;//月份

    public employee(double salary, String name) {
        this.salary = salary;
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public String getName() {
        return name;
    }

    public int getMonth() {
        return month;
    }
    public void print(){
        System.out.println(name+"的年工资="+(month*salary));
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

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

    public void setMonth(int month) {
        this.month = month;
    }
}
package com.charpter08.work05;

public class worker extends  employee {//子类
    public worker(double salary, String name) {
        super(salary, name);
    }
    public void print() {
        System.out.println("工人的工资:");
        super.print();
    }
}
package com.charpter08.work05;

public class Peasant extends employee {//农民
    public Peasant(double salary, String name) {
        super(salary, name);
    }
    public void print() {
        super.print();
    }
}
package com.charpter08.work05;

public class teacher extends employee {//老师
    private int classday;//一年上了多少天课
    private double classmoney;//一天多少钱

    public teacher(double salary, String name) {
        super(salary, name);
    }

    public void setClassday(int classday) {
        this.classday = classday;
    }

    public void setClassmoney(double classmoney) {
        this.classmoney = classmoney;
    }
    public void print() {
        System.out.println("老师的工资:");
        System.out.println(getName()+"的年工资="+(getSalary()*getMonth()+classmoney*classday));
    }
}

package com.charpter08.work05;

public class waiter extends employee {//服务生
    public waiter(double salary, String name) {
        super(salary, name);
    }
    public void print() {
        System.out.println("服务生的工资:");
        super.print();
    }
}
package com.charpter08.work05;

public class scientist extends employee {
    private  double yearbonus;//科学家年终奖

    public scientist(double salary, String name) {
        super(salary, name);
    }

    public double getYearbonus() {
        return yearbonus;
    }
    public void setYearbonus(double yearbonus) {
        this.yearbonus = yearbonus;
    }
    public void print() {
        System.out.println("科学家的工资:");
        System.out.println(getName()+"的年工资="+((getMonth()*getSalary())+yearbonus));
    }
}

设计一个point类,其x和其y坐标可以通过构造器提供,提供一个子类,其构造器接受一个标签值和x,y坐标

package com.charpter08.work09;

public class point {//父类
    private  double x;
    private  double y;

    public point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}
package com.charpter08.work09;

public class pointson extends  point {
    private String  label;//特有属性
    public pointson(double x, double y) {
        super(x, y);
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
    public void printl(){
        System.out.println("标签="+label+"\tx坐标="+getX()+"\ty坐标="+getY());
    }
}
package com.charpter08.work09;

public class text {
    public static void main(String[] args) {
        pointson pointson1 = new pointson(1929, 230.07);
        pointson1.setLabel("Thursday");
        pointson1.printl();
    }

}

! 程序运行图

房屋出租项目
项目设计——程序框架图(分层模式)模式的出现就是为了更加方便地管理
分层模式
简单的分层模式
简单的项目层级比较简单,后期大项目会有很多的层

当一个方法是static时,就是一个静态方法
静态方法可以直接通过类名调用

house类

package com.houserent;
/**
*house的对象表示一个房屋的信息
 */
public class house {
    //编号 房主 电话 地址 月租 状态
    private  int id;
    private  String name;
    private  String phone;
    private  String address;
    private  int rent;
    private  String state;

    public house(int id, String name, String phone, String address, int rent, String state) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.rent = rent;
        this.state = state;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
    public String toString() {
        return id +
                "\t" + name +
                "\t" + phone +
                "\t" + address +
                "\t" + rent +
                "\t" + state ;
                //编号 房主 电话 地址 月租 状态
    }
}

实现功能三部曲,明确完成功能——思路分析——代码实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值