Java学习第三十一天<对象的冒泡排序><老师类><员工工资打印1><员工工资打印2><测试题><银行存取款><判断属性><师生信息单><面向对象总结>

这篇博客涵盖了多个面向对象编程的实例,包括对象的冒泡排序,不同类型的教师类,员工工资打印,以及银行账户的存取款操作。通过这些例子,展示了类的继承、多态性、方法重写和动态绑定的概念。同时,还涉及了对象的比较和判断属性的方法,以及师生信息单的表示和排序。这些内容深入浅出地解释了面向对象编程的核心思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对象的冒泡排序

package chapter09.D2冒泡排序;
​
public class Homework01 {
    public static void main(String[] args) {
        Person[] p =new Person[3];
        p[0]=new Person("XX",56,"bb");
        p[1]=new Person("YY",25,"aa");
        p[2]=new Person("ZZ",47,"tt");
​
        Person temp=null;//对象也可以冒泡
        for (int i = 0; i < p.length-1 ; i++) {
            for (int j = 0; j < p.length-i-1 ; j++) {
                if (p[j].getAge()<p[j+1].getAge()) {//按年龄从大到小,名字长度p[j].getName().length()
                    temp=p[j];
                    p[j]=p[j+1];
                    p[j+1]=temp;
                }
            }
        }
        for (int i = 0; i <p.length ; i++) {
            System.out.println(p[i]);
        }
​
    }
}
class Person{
    private String name;
    private int age;
    private String job;
​
    public String getName() {
        return name;
    }
​
    public int getAge() {
        return age;
    }
​
    public String getJob() {
        return job;
    }
​
    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
​
    }
​
    @Override
    public String toString() {//重写toString,用于打印对象时打印信息
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }

老师类

package chapter09.D3老师类;
​
public class Homework02 {
    public static void main(String[] args) {
        Test test = new Test();
        Professor teacher1 = new Professor("xx", 45, "yy", 5000,1.3);
        test.test(teacher1);
        Vprofessor teacher2 = new Vprofessor("yy", 36, "tt", 5999, 1.2);
        test.test(teacher2);
        //编译类型可上转,因为接受的是父类
        Teacher teacher3 = new Lecturer("zz", 39, "pp", 2688, 1.1);
        test.test(teacher3);
        Teacher teacher4 = new Teacher("ff", 66, "oo", 366);
        test.test(teacher4);
    }
​
}
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 getName() {
        return name;
    }
​
    public int getAge() {
        return age;
    }
​
    public String getPost() {
        return post;
    }
​
    public double getSalary() {
        return salary;
    }
    public String info(){ //用toString方法,子类重写会有缺失,得不到属性,要手动this.属性
        return name+"年龄:"+age+"邮箱:"+post+"年薪:"+salary;
​
}
}
class Professor extends Teacher{
    private double salaryrank;
​
    public Professor(String name, int age, String post, double salary, double salaryrank) {
        super(name, age, post, salary);
        this.salaryrank = salaryrank;
    }
​
    @Override
    public String info() {
        return "教授-"+super.info()+"薪资等级:"+salaryrank;
    }
}
​
class Vprofessor extends Teacher{
    private double salaryrank;
​
    public Vprofessor(String name, int age, String post, double salary, double salaryrank) {
        super(name, age, post, salary);
        this.salaryrank = salaryrank;
    }
​
    @Override
    public String info() {
        return "副教授-"+super.info()+"薪资等级:"+salaryrank;
    }
}
class Lecturer extends Teacher{
    private double salaryrank;
​
    public Lecturer(String name, int age, String post, double salary, double salaryrank) {
        super(name, age, post, salary);
        this.salaryrank = salaryrank;
    }
​
    @Override
    public String info() {
        return "讲师-"+super.info()+"薪资等级:"+salaryrank;
    }
}
class Test{
    public void test(Teacher tea){//一定是父类
            System.out.println(tea.info());//父类子类都有info,不需要强转,动态绑定直接运行子类info
        }
    }

员工工资打印1

package chapter09.D4员工工资打印;
​
public class Homework03 {
    public static void main(String[] args) {
        A a = new A();
        Averemployee xx = new Averemployee("xx", 50, 10);
        Manager yy = new Manager("yy", 100, 20);
        a.print(xx);
        a.print(yy);
​
    }
}
class Employee{
    private String name;
    private double salary;
    private int days;
​
​
    public Employee(String name, double salary, int days) {
        this.name = name;
        this.salary = salary;
        this.days = days;
    }
​
    public String getName() {
        return name;
    }
​
    public double getSalary() {
        return salary;
    }
​
    public int getDays() {
        return days;
    }
​
    public double sum(){
        return salary*days;
    }
}
class Manager extends Employee{
​
    public Manager(String name, double salary, int days) {
        super(name, salary, days);
    }
​
    @Override
    public double sum() {
        return super.sum()*1.2+1000;
    }
}
class Averemployee extends Employee{
    public Averemployee(String name, double salary, int days) {
        super(name, salary, days);
    }
​
    @Override
    public double sum() {
        return super.sum();
    }
}
class A{
    public void print(Employee p){
        System.out.println(p.getName()+"的工资为"+ p.sum());
    }
}

员工工资打印2

package chapter09.D5员工工资打印2;
​
public class Emp {
    private double sal;
    private String name;
    private int Month;
​
    public Emp(double sal, String name) {
        this.sal = sal;
        this.name = name;
    }
​
    public int getMonth() {
        return Month;
    }
​
    public void setMonth(int month) {//月份可设置
        Month = month;
    }
​
    public String getName() {//子类调用名字
        return name;
    }
​
    public double Annual(){
        return sal*getMonth();
    }
}

package chapter09.D5员工工资打印2;
​
public class Peasant extends Emp{
    public Peasant(double sal, String name) {
        super(sal, name);
    }
​
    @Override
    public double Annual() {
        System.out.print("农民"+getName());
        return super.Annual();
    }
}

package chapter09.D5员工工资打印2;
​
public class Scientist extends Emp{
    private double pride;
​
    public Scientist(double sal, String name, double pride) {
        super(sal, name);
        this.pride = pride;
    }
​
    @Override
    public double Annual() {
        System.out.print("科学家"+getName()+"年薪为");
        return super.Annual()+pride;
    }
}

package chapter09.D5员工工资打印2;
​
public class Teacher extends Emp{
    private int classDay;
    private double classSal;
​
    public Teacher(double sal, String name, int classDay, double classSal) {
        super(sal, name);
        this.classDay = classDay;
        this.classSal = classSal;
    }
​
    @Override
    public double Annual() {
        System.out.print("老师"+getName());
        return super.Annual()+classDay*classSal;
    }
}

package chapter09.D5员工工资打印2;
​
public class Waiter extends Emp{
    public Waiter(double sal, String name) {
        super(sal, name);
    }
​
    @Override
    public double Annual() {
        System.out.print("服务员"+getName());
        return super.Annual();
    }
}

package chapter09.D5员工工资打印2;
​
public class Worker extends Emp{
    public Worker(double sal, String name) {
        super(sal, name);
    }
​
    @Override
    public double Annual() {
        System.out.print("工人"+getName());
        return super.Annual();
    }
}

package chapter09.D5员工工资打印2;
​
public class Homework03 {
    public static void main(String[] args) {
        B b = new B();
        Scientist scientist = new Scientist(5000,"xx",10000);
        scientist.setMonth(17);
        b.print(scientist);
        Teacher teacher = new Teacher(2000,"yy",360,1000);
        teacher.setMonth(12);
        b.print(teacher);
​
​
​
    }
}
class B{
    public void print(Emp e){
        System.out.println(e.Annual());
    }
}

测试题

package chapter09.D6测试题;
​
public class Demo extends Test{
    String name="Jack";
    Demo(){
        super();
        System.out.println("Demo");//2
    }
    Demo(String s){
        super(s);
    }
    public void test(){
        System.out.println(super.name);//3 Rose //john
        System.out.println(this.name);//4 jack //jack
    }
​
    public static void main(String[] args) {
        new Demo().test();
        new Demo("john").test();
    }
}

package chapter09.D6测试题;
​
public class Test {
    String name="Rose";
    Test(){
        System.out.println("Test");//1
    }
    Test(String name){
        this.name=name;//属性没有动态绑定,故会把"Rose"改成john
    }
}

银行存取款

package chapter09.D7银行存取款;
​
public class BankAccount {
    private double balance;
    public BankAccount(double initialBalance){
        this.balance=initialBalance;
    }
    public void deposit(double amount){
        balance+=amount;
    }
    public void withdraw(double amount){
        balance-=amount;
    }
​
    public double getBalance() {
        return balance;
    }
​
    public void setBalance(double balance) {
        this.balance = balance;
    }
}
class CheckingAccount extends BankAccount{//每次存款取款收1美元手续费
    public CheckingAccount(double initialBalance) {
        super(initialBalance);
    }
​
    @Override
    public void deposit(double amount) {
        super.deposit(amount-1);
    }
​
    @Override
    public void withdraw(double amount) {
        super.withdraw(amount+1);
    }
}
​
class SavingAccount extends BankAccount{
    public SavingAccount(double initialBalance) {
        super(initialBalance);
    }
    int conut=3;
​
    @Override
    public void deposit(double amount) {
        if (conut >0) {
            super.deposit(amount);
            conut--;
        }else {
            super.deposit(amount-1);
        }
​
    }
​
    @Override
    public void withdraw(double amount) {
        if (conut>0){
            super.withdraw(amount);
            conut--;
        }else {
            super.withdraw(amount+1);
        }
​
    }
    public void earnMonthlyInterest(){
        super.setBalance(getBalance()*1.1);
        conut=3;
    }
}

package chapter09.D7银行存取款;
​
public class Homework04 {
    public static void main(String[] args) {
        SavingAccount savingAccount = new SavingAccount(5000);
        savingAccount.withdraw(300);
        System.out.println(savingAccount.getBalance());
        savingAccount.deposit(500);
        savingAccount.withdraw(300);
        System.out.println(savingAccount.getBalance());
        savingAccount.withdraw(200);
        System.out.println(savingAccount.getBalance());
        savingAccount.earnMonthlyInterest();
        System.out.println(savingAccount.getBalance());
        savingAccount.withdraw(168.9);
        savingAccount.withdraw(200);
        savingAccount.withdraw(300);
        System.out.println(savingAccount.getBalance());
        savingAccount.deposit(500);
        System.out.println(savingAccount.getBalance());
        System.out.println("===============================");
        CheckingAccount checkingAccount = new CheckingAccount(5000);
        checkingAccount.deposit(600);
        System.out.println(checkingAccount.getBalance());
        checkingAccount.withdraw(400);
        System.out.println(checkingAccount.getBalance());
    }
}

判断属性

package chapter09.D8判断属性;
​
import java.util.Objects;
​
public class Doctor {
    private String name;
    private int age;
    private String job;
    private char gender;
    private double sal;
​
    public Doctor(String name, int age, String job, char gender, double sal) {
        this.name = name;
        this.age = age;
        this.job = job;
        this.gender = gender;
        this.sal = sal;
    }
​
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;//getClass()查看运行类型
        Doctor doctor = (Doctor) o;
        return age == doctor.age && gender == doctor.gender && Double.compare(doctor.sal, sal) == 0 && Objects.equals(name, doctor.name) && Objects.equals(job, doctor.job);
    }
​
}

package chapter09.D8判断属性;
​
public class Test {
    public static void main(String[] args) {
        Doctor d1 = new Doctor("xx", 40, "tt", '男', 200);
        Doctor d2 = new Doctor("xx", 40, "tt", '男', 200);
        System.out.println(d1.equals(d2));
​
    }
}

师生信息单

package chapter09.D9师生信息单;
​
public class Person {
    private String name;
    private char sex;
    private int age;
​
    public Person(String name, char sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
​
    public String getName() {
        return name;
    }
​
    public char getSex() {
        return sex;
    }
​
    public int getAge() {
        return age;
    }
​
    public String play(){
        return "爱玩";
    }
}

package chapter09.D9师生信息单;
​
public class Student extends Person{
    private int ID;
​
    public Student(String name, char sex, int age, int ID) {
        super(name, sex, age);
        this.ID = ID;
    }
​
    public int getID() {
        return ID;
    }
​
    public void study(){
        System.out.println("我承诺,我会好好学习。");
    }
​
    @Override
    public String play() {
        return getName()+super.play()+"足球";
    }
    public void print(){
        System.out.println("学生的信息:\n姓名:"+getName()+"\n年龄:"+getAge()+"\n性别:"+getSex()+"\n学号:"+ID);
    }
}

package chapter09.D9师生信息单;
​
public class Teacher extends Person{
    private int wage;
​
    public Teacher(String name, char sex, int age, int wage) {
        super(name, sex, age);
        this.wage = wage;
    }
​
​
    public int getWage() {
        return wage;
    }
​
    public void teach(){
        System.out.println("我承诺,我会好好教学。");
    }
​
    @Override
    public String play() {
        return getName()+super.play()+"象棋";
    }
    public void print(){
        System.out.println("老师的信息:\n姓名:"+getName()+"\n年龄:"+getAge()+"\n性别:"+getSex()+"\n工龄:"+getWage());
    }
}

package chapter09.D9师生信息单;
​
public class Homework05 {
    public static void main(String[] args) {
        Homework05 homework05 = new Homework05();
        Student xx = new Student("xx", '男', 20, 12345);
        homework05.info(xx);
        System.out.println("-------------------------------");
        Teacher yy = new Teacher("yy", '女', 29, 5);
        homework05.info(yy);
        //多态数组+冒泡排序
        Person []pers =new Person[4];
        pers[0] = new Student("zz", '男', 26, 4569);
        pers[1] = new Teacher("ww", '女', 36, 6);
        pers[2] = new Student("tt", '女', 21, 5445);
        pers[3] = new Teacher("qq", '女', 39, 7);
        Person temp=null;
        for (int i = 0; i < pers.length-1 ; i++) {
            for (int j = 0; j < pers.length-i-1 ; j++) {
                if (pers[j].getAge()<pers[j+1].getAge()) {
                    temp=pers[j+1];
                    pers[j+1]=pers[j];
                    pers[j]=temp;
                }
            }
        }
        for (int i = 0; i < pers.length ; i++) {
            System.out.println("-------------------------------");
            homework05.info(pers[i]);
        }
​
​
    }
    public void info(Person p){
        if (p instanceof Student){
            ((Student) p).print();
            ((Student) p).study();
            System.out.println(p.play());
        }else if (p instanceof Teacher){
            ((Teacher) p).print();
            ((Teacher) p).teach();
            System.out.println(p.play());
        }
    }
​
}

面向对象总结

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值