P343java三大特性作业-P349练习作业8

P343java三大特性作业-P350练习作业8

P343冒泡排序数组(自己写的)

package com.work;
​
public class Test {
    public static void main(String[] args) {
        Person [] arr=new Person [3];
        arr[0]=new Person("aa",22,"jb0");
        arr[1]=new Person("bb",21,"jb1");
        arr[2]=new Person("cc",20,"jb2");
        new Person ().sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
​
​
    }
}
class Person{
   private String name;
   private int age;
   private String job;
​
    public Person() {
​
    }
​
    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }
    public void sort(Person [] arr){
       Person temp= null;
        for (int i = arr.length-1; i <1 ; i--) {
            if(arr[i].age > arr[i-1].age){
                temp=arr[i-1];
                arr[i-1]=arr[i];
                arr[i]=temp;
            }else{
​
            }
        }
    }
​
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public int getAge() {
        return age;
    }
​
    public void setAge(int age) {
        this.age = age;
    }
​
    public String getJob() {
        return job;
    }
​
    public void setJob(String job) {
        this.job = job;
    }
}

P344访问修饰符

本类同包子类不同包
publicvvvv
protectedvvv
默认vv
privatev
package com.work;
//P345,自己写的
​
public class Homework03 {
    public static void main(String[] args) {
        Teacher t1 = new Professor("aa","job0",20,9999.0,1.3);
        t1.introduce();
        Teacher t2 = new AP("bb","job1",21,8999.0,1.2);
        t2.introduce();
        Teacher t3 = new CP("cc","job2",22,7999.0,1.1);
        t3.introduce();
    }
​
}
class CP extends Teacher{
    private double ca=1.1;
    public void introduce(){
        System.out.println(super.toString()+"\tca=\t"+ca);
    }
​
    public CP(String name, String post, int age, double salary, double ca) {
        super(name, post, age, salary);
        this.ca = ca;
    }
​
    public double getCa() {
        return ca;
    }
​
    public void setCa(double ca) {
        this.ca = ca;
    }
}
​
​
class AP extends Teacher{
    private double ca=1.2;
    public void introduce(){
        System.out.println(super.toString()+"\tca=\t"+ca);
    }
​
    public AP(String name, String post, int age, double salary, double ca) {
        super(name, post, age, salary);
        this.ca = ca;
    }
​
    public double getCa() {
        return ca;
    }
​
    public void setCa(double ca) {
        this.ca = ca;
    }
}
class Professor extends Teacher{
    private double ca=1.3;
    public void introduce(){
        System.out.println(super.toString()+"\tca\t"+ca);
    }
​
    public Professor(String name, String post, int age, double salary, double ca) {
        super(name, post, age, salary);
        this.ca = ca;
    }
​
    public double getCa() {
        return ca;
    }
​
    public void setCa(double ca) {
        this.ca = ca;
    }
}
​
class Teacher{
    private String name;
    private String post;
    private int age;
    private double salary;
​
    public Teacher() {
    }
​
    public Teacher(String name, String post, int age, double salary) {
        this.name = name;
        this.post = post;
        this.age = age;
        this.salary = salary;
    }
​
    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", post='" + post + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
    public void introduce(){
        System.out.println(toString());
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public String getPost() {
        return post;
    }
​
    public void setPost(String post) {
        this.post = post;
    }
​
    public int getAge() {
        return age;
    }
​
    public void setAge(int age) {
        this.age = age;
    }
​
    public double getSalary() {
        return salary;
    }
​
    public void setSalary(double salary) {
        this.salary = salary;
    }
}

P346作业4自己做的

package com.work;
​
public class Homework03 {
    public static void main(String[] args) {
       M m= new M("1n",1111.0,2);
        //设置奖金,感觉老师的思路比较合理
        m.setBound(1000);
        m.prints();
       new C("2n",300.0,10).prints();
    }
}
class C extends EM{
    public C(String name, double daysalary, int day) {
        super(name, daysalary, day);
    }
    public void prints() {
        double s=getDay()*getDaysalary()*1.1;
        System.out.println("name"+getName()+"工资="+s);
    }
}
class M extends EM{
    //奖金可能是不一定的
    private double bound;
    @Override
    public void prints() {
        double s=getBound()+getDay()*getDaysalary()*1.2;
        System.out.println("name"+getName()+"工资="+s);
    }
​
    public M(String name, double daysalary, int day) {
        super(name, daysalary, day);
    }
​
    public double getBound() {
        return bound;
    }
​
    public void setBound(double bound) {
        this.bound = bound;
    }
}
class EM{
    private String name;
    private double daysalary;
    private int day;
    public void prints(){
        double s=day*daysalary;
        System.out.println("name"+name+"工资="+s);
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public double getDaysalary() {
        return daysalary;
    }
​
    public void setDaysalary(double daysalary) {
        this.daysalary = daysalary;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public EM(String name, double daysalary, int day) {
        this.name = name;
        this.daysalary = daysalary;
        this.day = day;
    }
}

P347作业5

package com.work;
public class Homework03 {
    public static void main(String[] args) {
            T t = new T("teacher1",6666);
            t.setDsal(1111);
            t.prints();
            S s = new S("scientist1",8888);
            s.setBound(111);
            s.prints();
    }
}
class T extends EM{//老师
    private double dsal;
    @Override
    public void prints() {
        double s=getCountsal()+getDsal();
        System.out.println("老师\t"+getName()+"\t工资="+s);
    }
    public T(String name, double countsal) {
        super(name, countsal);
    }
    public double getDsal() {
        return dsal;
    }
    public void setDsal(double dsal) {
        this.dsal = dsal;
    }
}
class S extends EM{//科学家
    //奖金可能是不一定的
    private double bound;//年终奖
    @Override
    public void prints() {
        double s=getCountsal()+getBound();
        System.out.println("科学家\t"+getName()+"\t工资="+s);
    }
    public S(String name, double countsal) {
        super(name, countsal);
    }
    public double getBound() {
        return bound;
    }
    public void setBound(double bound) {
        this.bound = bound;
    }
}
class W extends EM{//工人
    public W(String name, double countsal) {
        super(name, countsal);
    }
    public void prints(){
        System.out.println("工人\t"+getName()+"\t工资="+getCountsal());
    }
}
class EM{
    private String name;
    private double countsal;
    public void prints(){
        System.out.println("员工=\t"+name+"\t工资="+countsal);
    }
    public EM(String name, double countsal) {
        this.name = name;
        this.countsal = countsal;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getCountsal() {
        return countsal;
    }
    public void setCountsal(double countsal) {
        this.countsal = countsal;
    }
}
​

P348作业6

image-20220101185455804

--

总结一下

super关键字先找父类,父类没有找源类

this从本类开始,本类没有找父类

P349作业7(第二个没做对)

第一个 test ,Domo, rose jack

第二个父类的构造器 把传入的john,this.name=name意思是john代替rose,因为属性没有动态绑定rose

test.()开始第一个是super.name,输出父类的属性为john,后面的this.name是本类的jack

image-20220101191733291

--

P350作业8老师第二问写很好细看

下面是源类

class BA{
    private double balance;//余额
​
    public BA(double inB) {
        this.balance = inB;
    }
    public void deposite(double amout){//存款
        balance += amout;
    }
    public void withdraw(double amout){
        balance -=amout;
    }
​
    public double getBalance() {
        return balance;
    }
​
    public void setBalance(double balance) {
        this.balance = balance;
    }
}

取款和存款都要手续费1块

class CA extends BA {//收取手续费
    private double check;
​
    public void deposite(double amout) {//重写
        super.deposite(amout - 1);//老师的方法更简单
    }
​
    public void withdraw(double amout) {
        super.withdraw(amout - 1);
    }
}

每月三次免手续费,每月利息

class EA extends BA {//利息
    private double interest=0.01;//利率
    private int count=3;
    public void eMt(){//每个月初统计利息,且把count=3
        count=3;
        super.deposite(getBalance()*interest);//这个地方很巧妙
        //把利息当成存款计利息,调用父类的方法,没有用count的次数
    }
    public void deposite(double amout){
        if(count >0){
            super.deposite(amout);
        }else{
            super.deposite(amout-1);
        }
        count--;
    }
    public void withdraw(double amout){
        if(count >0){
            super.withdraw(amout);
        }else{
            super.withdraw(amout+1);
        }
        count--;
​
    }
    public int getCount() {
        return count;
    }
​
    public void setCount(int count) {
        this.count = count;
    }
    
    public double getInterest() {
        return interest;
    }
​
    public void setInterest(double interest) {
        this.interest = interest;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值