类型一
定义一个Person类(name,age,job),初始化Person对象数组,有3个Person对象,并按照age从大到小进行排序
public class Homework01 {
public static void main(String[] args) {
//定义一个Person类(name,age,job),初始化Person对象数组,
//有3个Person对象,并按照age从大到小进行排序
//对象初始化
Person p1 = new Person("张三", 30, "老师");
Person p2 = new Person("李四", 50, "校长");
Person p3 = new Person("铁蛋", 10, "学生");
//创建数组
Person[] p = {p1, p2, p3};
//输出当前对象数组
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
System.out.println("=====排序后=====");
Person n1 = null;//临时变量,用于交换
int n2 = p.length - 1;//表示数组的下标
//冒泡排序
for (int i = 0; i < n2; i++) {
for (int j = 0; j < n2 - i; j++) {
//如果前面的age小于后面的age就进行交换
if(p[i].age < p[i + 1].age) {
n1 = p[i];
p[i] = p[i + 1];
p[i + 1] = n1;
}
}
}
//输出结果
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
}
}
class Person {
String name;
int age;
String job;
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", job='" + job + '\'' +
'}';
}
}
输出:

练习二
写出四种访问修饰符和各自的访问权限
class Homework02 {
//写出四种访问修饰符和各自的访问权限
public int n1 = 10;
//访问权限:1.同类 2.同包 3.子类 4.不同包
protected int n2 = 20;
//访问权限:1.同类 2.同包 3.子类
int n3 = 30;
//访问权限:1.同类 2.同包
private int n4 = 40;
//访问权限:1.同类
}
练习三
编写老师类
要求有属性 名字name,年龄age,职位post,基本工资salary
编写业务方法,introduce(),实现输出一个教师的信息
编写教师类的三个子类:教授类(Professor)、副教授、讲师类工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce方法
定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印
public class Homework03 {
public static void main(String[] args) {
Professor professor = new Professor("张三", 50, "教授", 9000, 1.3);
Assprofessor assprofessor = new Assprofessor("李四", 40, "副教授", 7000, 1.2);
Lecturer lecturer = new Lecturer("老王", 28, "讲师", 5000, 1.1);
professor.introduce();
assprofessor.introduce();
lecturer.introduce();
}
}
class Teacher {
private String name;
private int age;
private String post;
private double salary;
private double grade;
public Teacher(String name, int age, String post, double salary, double grade) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
this.grade = grade;
}
public Teacher(String name, int age, String post, double salary) {
}
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 getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public void introduce() {
System.out.println("名字=" + name + " 年龄=" + age + " 职位=" +
post + " 基本工资=" + salary + " 工资级别=" + grade);
}
}
class Professor extends Teacher{
public Professor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
super.introduce();
}
}
class Assprofessor extends Teacher {
public Assprofessor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
super.introduce();
}
}
class Lecturer extends Teacher {
public Lecturer(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
super.introduce();
}
}

练习四
通过继承实现员工工资核算打印功能
父类:员工类
子类:部门经理类、普通员工类
部门经理工资 = 1000 + 单日工资 * 天数 * 等级(1.2)
普通员工工资 = 单日工资 * 天数 * 等级(1.1)
员工属性:姓名,单日工资,工作天数
员工方法:打印工资
普通员工和经理部门但是员工的子类,需要重写打印工资方法
定义并初始化员工对象,调用打印工资的方法
public class Homework04 {
public static void main(String[] args) {
Manger manger = new Manger("张三", 500, 30, 1.2);
//设置奖金
manger.setBonus(1000);
manger.wages();
OrEmployees orEmployees = new OrEmployees("李四", 200, 30, 1.1);
orEmployees.wages();
}
}
class Employee {
//属性
private String name;
private double salary;
private int days;
private double grade;
public Employee(String name, double salary, int days, double grade) {
this.name = name;
this.salary = salary;
this.days = days;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
//打印工资
public void wages() {
System.out.println(name + "的工资是=" + (salary * days * grade));
}
}
class Manger extends Employee {
//特有属性,奖金
private double bonus;
public Manger(String name, double salary, int days, double grade) {
super(name, salary, days, grade);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//重写父类方法
@Override
public void wages() {
System.out.println("经理");
System.out.println(getName() + "的工资是=" + (bonus + getSalary() *
getDays() * getGrade()));
}
}
class OrEmployees extends Employee {
public OrEmployees(String name, double salary, int days, double grade) {
super(name, salary, days, grade);
}
@Override
public void wages() {
System.out.println("普通员工");
super.wages();
}
}

练习五
设计父类:员工类
子类:工人类,农民类,教师类,科学家类,服务员类
其中工人,农民,服务生,只有基本工资
教师除基本工资外,还有课酬(元/天)
科学家除基本工资外,还有年终奖
将各种类型的员工的全年工资打印出来
public class Homework05 {
public static void main(String[] args) {
Worker worker = new Worker("工人", 10000);
worker.Annsalary();
Peasant peasant = new Peasant("农民", 3000);
peasant.Annsalary();
Waiter waiter = new Waiter("服务员", 4000);
waiter.Annsalary();
Teacher05 teacher05 = new Teacher05("教师", 5000);
teacher05.setDaysal(60);
teacher05.Annsalary();
Scientist scientist = new Scientist("科学家", 30000);
scientist.setAnnsalary(50000);
scientist.Annsalary();
}
}
class Employee05 {
//基本工资
private String name;
private double salary;
public Employee05(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//计算全年工资
public void Annsalary() {
System.out.println(name + " 的年工资是= " + (salary * 12));
}
}
//工人
class Worker extends Employee05{
public Worker(String name, double salary) {
super(name, salary);
}
@Override
public void Annsalary() {
super.Annsalary();
}
}
//农民
class Peasant extends Employee05 {
public Peasant(String name, double salary) {
super(name, salary);
}
@Override
public void Annsalary() {
super.Annsalary();
}
}
//服务员
class Waiter extends Employee05 {
public Waiter(String name, double salary) {
super(name, salary);
}
@Override
public void Annsalary() {
super.Annsalary();
}
}
//教师
class Teacher05 extends Employee05 {
//特有属性课酬
private double daysal;
public Teacher05(String name, double salary) {
super(name, salary);
}
public double getDaysal() {
return daysal;
}
public void setDaysal(double daysal) {
this.daysal = daysal;
}
@Override
public void Annsalary() {
System.out.println(getName() + " 的年工资是=" +
((daysal * 30) + getSalary()) * 12);
}
}
//科学家
class Scientist extends Employee05 {
//特有属性年终奖
private double annsalary;
public Scientist(String name, double salary) {
super(name, salary);
}
public double getAnnsalary() {
return annsalary;
}
public void setAnnsalary(double annsalary) {
this.annsalary = annsalary;
}
@Override
public void Annsalary() {
System.out.println(getName() + " 的年工资是=" +
((getSalary() * 12) + annsalary));
}
}
练习六

练习七

输出结果:
Test
Demo
Rose
Jack
john
Jack
练习八

public class Homework08 {
public static void main(String[] args) {
// CheckingAccount ch = new CheckingAccount(1000);
// ch.deposit(500);
// ch.withdraw(500);
// ch.cbalance();
SavingsAccount sa = new SavingsAccount(1000);
sa.deposit(10);
sa.deposit(10);
sa.deposit(10);
System.out.println(sa.getBalance());
sa.deposit(10);
System.out.println(sa.getBalance());
//月初自动调用earnMonthlyInterest方法
sa.earnMonthlyInterest();//重置手续费次数,增加利息
System.out.println(sa.getBalance());
sa.withdraw(100);
System.out.println(sa.getBalance());
}
}
class BankAccount {
//余额
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//存款
public void deposit(double amount) {
balance += amount;
}
//取款
public void withdraw(double amount) {
balance -= amount;
}
}
class CheckingAccount extends BankAccount {
public CheckingAccount(int balance) {
super(balance);
}
@Override
public void deposit(double amount) {
super.deposit(amount - 1);
}
@Override
public void withdraw(double amount) {
super.withdraw(amount + 1);
}
public void cbalance() {
System.out.println("当前余额为:" + super.getBalance());
}
}
class SavingsAccount extends BankAccount {
//特有属性
private int count = 3;
private double rate = 0.01;//利率
public SavingsAccount(double balance) {
super(balance);
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
@Override
public void deposit(double amount) {
//判断是否可以免手续费
if (count > 0) {
super.deposit(amount);
}else {
super.deposit(amount - 1);
}
count--;//减去一次
}
@Override
public void withdraw(double amount) {
//判断是否可以免手续费
if (count > 0) {
super.withdraw(amount);
}else {
super.withdraw(amount + 1);
}
count--;//减去一次
}
//每个月初重置免手续费次数,统计上个月的利息
public void earnMonthlyInterest() {
count = 3;
super.deposit(getBalance() * rate);
}
}
练习九
设计一个Point类,其x和y坐标可以通过构造器提供。提供一个子类LabeledPoint,其更准确接受一个标签值和x,y坐标,写出对应的构造器即可
public class Homework09 {
public static void main(String[] args) {
LabeledPoint black = new LabeledPoint("Black Thursday", 1929, 230.07);
}
}
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 void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
class LabeledPoint extends Point {
private String black;
public LabeledPoint( String black, double x, double y) {
super(x, y);
this.black = black;
}
}
练习十
编写Doctor类{name,age,iob,gender,sal},重写父类的euqals方法
判断测试类中创建的两个对象是否相等,属性是否相同
public class Homework10 {
public static void main(String[] args) {
Doctor d1 = new Doctor("张三", 20, "老师", '男', 5000);
Doctor d2 = new Doctor("张三", 20, "老师", '男', 5000);
System.out.println(d1 == d2);
System.out.println(d1.equals(d2));
}
}
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;
}
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;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public boolean equals(Object obj) {
//判断两个对象是否是同一个
if (this == obj) {
return true;
}
//判断是否是Doctor类型或者是其子类
if (!(obj instanceof Doctor)) {//不是的话
return false;
}
//向下转型
Doctor d = (Doctor) obj;
return this.name == d.name && this.age == d.age && this.job == d.job &&
this.gender == d.gender && this.sal == d.sal;
}
}
练习十一

public class Homework11 {
public static void main(String[] args) {
//向上转型
Person11 p = new Student();
p.run();//方法看编译类型
p.eat();
//向下转型
Student s = (Student) p;
s.run();
s.study();
s.eat();
}
}
class Person11 {
public void run() {
System.out.println("person run");
}
public void eat() {
System.out.println("person eat");
}
}
class Student extends Person11 {
public void run() {
System.out.println("student run");
}
public void study() {
System.out.println("student study");
}
}

练习十二

练习十三
public class Homework13 {
public static void main(String[] args) {
Homework13 h = new Homework13();
Person13[] p = new Person13[4];
p[0] = new Teacher13("张三", '男', 30, "足球", 5);
p[1] = new Teacher13("老六", '男', 50, "象棋", 20);
p[2] = new Student13("铁蛋", '男', 15, "羽毛球", "20220202");
p[3] = new Student13("小六", '女', 17, "游戏", "20220209");
//7.按照年龄大小进行排序
Person13 n1 = null;//定义临时变量,用于交换
for (int i = 0; i < p.length - 1; i++) {//外层循环
for (int j = 0; j < p.length -1 - i; j++) {
//判断如果前面的年龄小于后面的年龄就交换
if (p[j].getAge() < p[j + 1].getAge()) {
n1 = p[j];
p[j] = p[j + 1];
p[j + 1] = n1;
}
}
}
//输出信息
for (int i = 0; i < p.length; i++) {
p[i].sae();
h.tese(p[i]);
System.out.println(p[i].play());
System.out.println("-----------------");
}
}
//8.定义方法,形参为Person类型 功能:调用老师的teach方法或学生的study方法
public void tese(Person13 p) {
if (p instanceof Teacher13) {
((Teacher13) p).teach();
}else {
((Student13) p).study();
}
}
}
class Person13 {
private String name;
private char sex;
private int age;
private String hobby;
public Person13(String name, char sex, int age, String hobby) {
this.name = name;
this.sex = sex;
this.age = age;
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public void sae() {
System.out.println("姓名:" + name + "\n" + "年龄:" + age + "\n" +
"性别:" + sex);
}
public String play() {
return name + "爱玩" + hobby;
}
}
class Teacher13 extends Person13 {
//工龄
private int workage;
public Teacher13(String name, char sex, int age, String hobby, int workage) {
super(name, sex, age, hobby);
this.workage = workage;
}
public int getWorkage() {
return workage;
}
public void setWorkage(int workage) {
this.workage = workage;
}
//教学方法
public void teach() {
System.out.println("我承诺,我会认真教学");
}
@Override
public void sae() {
System.out.println("老师的信息如下");
super.sae();
System.out.println("工龄:" + workage);
// teach();
// System.out.println(play());
}
@Override
public String play() {
return super.play();
}
}
class Student13 extends Person13 {
//学号
private String stuid;
public Student13(String name, char sex, int age, String hobby, String stuid) {
super(name, sex, age, hobby);
this.stuid = stuid;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
//学习方法
public void study() {
System.out.println("我承诺,我会好好学习");
}
@Override
public void sae() {
System.out.println("学生的信息如下");
super.sae();
System.out.println("学号:" + stuid);
// study();
// System.out.println(play());
}
}

练习十四

输出:
我是A类
hahah我是B类有参构造器
我是C类的有参构造器
我是C类的无参构造器
练习十五
什么是多态,多态具体体现有哪些?
多态:方法或对象具体多种形态,是面向对象(OOP)的第三大特征,是建立在封装和继续的基础之上
多态的具体体现:
1、方法多态:
重载和重写体现多态
2、对象多态:
1.一个对象的编译类型和运行类型可以不一致。
2.编译类型在定义对象时,就确定了,不能改变
3.运行类型是可以变化的。
4.编译类型看定义时 = 号的左边,运行类型看 = 号的右边。
举例说明:
public class Homework15 {
public static void main(String[] args) {
AA obj = new BB();//向上转型
System.out.println("obj的运行类型=" + obj.getClass());//BB
obj = new CC();//向上转型
System.out.println("obj的运行类型=" + obj.getClass());//CC
AA b1 = obj;
System.out.println("obj的运行类型=" + obj.getClass());//BB
}
}
class AA {//超类
}
class BB extends AA {//父类
}
class CC extends BB {//子类
}
练习十六
Java的动态绑定机制是什么?
1、当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定
2、当调用对象属性时,没有动态绑定机制,哪里声明,那里使用。
这篇博客主要介绍了Java编程的学习练习,包括Person类的排序、访问修饰符、教师类及其子类的设计、员工工资计算、多态概念及应用、动态绑定机制等。通过一系列的实例和代码演示,深入探讨了Java编程的基础知识和核心概念。


355

被折叠的 条评论
为什么被折叠?



