系列文章目录
作业
作业1
代码
(1)自己写的
代码如下:
package studyhan.demo1;
public class Hello {
//编写一个main方法
// 测试
public static void main(String[] args) {
Person jack = new Person("jack", 45,"teacher");
Person marry = new Person("marry", 50, "doctor");
Person tom = new Person("tom", 35, "manager");
Person people[] = {jack,marry,tom};
Person young = null;
for(int i = 0;i< people.length-1;i++){
for(int j = 0;j< people.length-1-i;j++){
if(people[j].getAge()<people[j+1].getAge()){
young = people[j];
people[j]=people[j+1];
people[j+1] = young;
}
}
}
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
}
}
class Person {
private String name;
private int age;
private 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 + '\'' +
'}';
}
public int getAge(){
return age;
}
}
结果如下:
(2)老师写的
代码如下:
package com.hspedu.homework;
public class Homework01 {
public static void main(String[] args) {
//初始化Person 对象数组,有3个person对象
Person[] persons = new Person[3];
persons[0] = new Person("mary",30, "PHP工程师");
persons[1] = new Person("tom",50, "大数据工程师");
persons[2] = new Person("smith",10, "JavaEE工程师");
//输出当前对象数组
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);//默认对象的.toString()
}
//使用冒泡排序
Person tmp = null;//临时变量,用于交换
for(int i = 0; i < persons.length -1 ;i++) {//外层循环
for(int j = 0; j < persons.length -1 - i; j++) {//内层循环
//并按照 age 从 大到 小进行排序, 如果前面的人的age < 后面人的年龄,就交换
//要求按照名字的长度从小到大 if(persons[i].getName().length() > persons[i+1].getName().length())
if(persons[j].getAge() > persons[j+1].getAge()) {
tmp = persons[j];
persons[j] = persons[j+1];
persons[j+1]= tmp;
}
}
}
System.out.println("排序后的效果");
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);//默认对象的.toString()
}
}
/*
定义一个Person类 {name, age, job}, 初始化Person 对象数组,有3个person对象,
并按照 age 从 大到 小进行排序, 提示,使用冒泡排序
*/
}
class Person {
private String name;
private int age;
private String job;
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", job='" + job + '\'' +
'}';
}
}
作业2
public 同类、同包、子类、不同包
protected 同类、同包、子类
默认 同类、同包
private 同类
作业3
代码
(1)老师写的+自己写的
只写了教授类,其他两个类与其相同。
代码如下:
package studyhan.demo1;
public class Hello {
//编写一个main方法
// 测试
public static void main(String[] args) {
Professor professor = new Professor("jack", 29, "高级职称", 23000, 1.3);
professor.introduce();
}
}
class Teacher {
private String name;
private int age;
private String post;
private double salary;
private double scale;
public Teacher(String name, int age, String post, double salary, double scale) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
this.scale = scale;
}
public void introduce() {
System.out.println("name:" + name + "\t" + "age:" + age + "\t" + "post:" + post + "\t" + "salary:" + salary + "\t" + "scale:" + scale);
}
}
class Professor extends Teacher {
public Professor(String name, int age, String post, double salary, double scale) {
super(name, age, post, salary, scale);
}
@Override
public void introduce() {
System.out.println("这是教授信息:");
super.introduce();
}
}
结果如下:
作业4
代码
(1)自己写的
代码如下:
package studyhan.demo1;
public class Hello {
//编写一个main方法
// 测试
public static void main(String[] args) {
Manager jack = new Manager("jack", 120, 1.2, 30, 2000);
Worker marry = new Worker("marry", 100, 1.0, 31);
jack.printSalary();
marry.printSalary();
}
}
class Employee {
private String name;
private double singlesalary;
private double scale;
private int day;
public Employee(String name, double singlesalary, double scale, int day) {
this.name = name;
this.singlesalary = singlesalary;
this.scale = scale;
this.day = day;
}
public String getName() {
return name;
}
public double getSinglesalary() {
return singlesalary;
}
public double getScale() {
return scale;
}
public int getDay() {
return day;
}
public void printSalary() {//打印工资说明无返回值
System.out.println("工资" + singlesalary * day * scale);
}
}
class Manager extends Employee {
private double bonus;
public Manager(String name, double singlesalary, double scale, int day, double bonus) {
super(name, singlesalary, scale, day);
this.bonus = bonus;
}
public void printSalary() {
System.out.println("部门经理工资" + (super.getSinglesalary() * super.getDay() * super.getScale() + bonus));
}
}
class Worker extends Employee {
public Worker(String name, double singlesalary, double scale, int day) {
super(name, singlesalary, scale, day);
}
public void printSalary() {
System.out.println("普通员工工资" + super.getSinglesalary() * super.getDay() * super.getScale());
}
}
注:Manager的奖金放在构造器初始化了,一般奖金是变化的,最好将奖金放在set方法中。
结果如下:
(2)老师写的
代码如下:
package com.hspedu.homework;
public class Homework04 {
public static void main(String[] args) {
Manager manage = new Manager("刘备", 100, 20, 1.2);
//设置奖金
manage.setBonus(3000);
//打印经理的工资情况
manage.printSal();
Worker worker = new Worker("关羽",50, 10, 1.0);
worker.printSal();
}
}
package com.hspedu.homework;
import java.sql.SQLOutput;
public class Employee {
//属性
//员工属性:姓名,单日工资,工作天数
private String name;
private double daySal;
private int workDays;
//分析出还有一个属性等级
private double grade;
//方法[构造器,getter 和 setter]
//打印工资方法
//方法 void printSal() {}
public void printSal() {
System.out.println(name + " 工资=" + daySal * workDays * grade);
}
public Employee(String name, double daySal, int workDays, double grade) {
this.name = name;
this.daySal = daySal;
this.workDays = workDays;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDaySal() {
return daySal;
}
public void setDaySal(double daySal) {
this.daySal = daySal;
}
public int getWorkDays() {
return workDays;
}
public void setWorkDays(int workDays) {
this.workDays = workDays;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
package com.hspedu.homework;
public class Manager extends Employee {
//特有属性
private double bonus;
//创建Manager对象时,奖金是多少并不是确定的,所以老师在构造器中,不给bonus
//,可以通过setBonus
public Manager(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
//方法:重写父类的 printSal
@Override
public void printSal() {
//因为经理的工资计算方式和Employee不一样,所以我们重写
System.out.println("经理 " + getName() + " 工资是="
+ (bonus + getDaySal() * getWorkDays() * getGrade()));
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
创建Manager对象时,奖金是多少并不是确定的,所以老师在构造器中,不给bonus,可以通过setBonus方法设置奖金。
package com.hspedu.homework;
public class Worker extends Employee{
//分析普通员工没有特有的属性
public Worker(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
//重写printSal
//因为普通员工和Employee输出工资情况一下,所以直接调用父类的printSal()
@Override
public void printSal() {
System.out.print("普通员工 ");//自己的输出信息
super.printSal();//调用父类的方法,复用代码
}
}
作业5
代码
(1)自己写的(只用了构造器初始化值,没有get、set方法灵活)
代码如下:
package studyhan.demo1;
public class Hello {
//编写一个main方法
// 测试
public static void main(String[] args) {
Worker jack = new Worker("jack", 4500);
Peasant marry = new Peasant("marry", 3000);
Teacher jim = new Teacher("jim", 3000, 100.0, 31);
Scientist lili = new Scientist("lili", 10000, 20000);
Waiter sunny = new Waiter("sunny", 3500);
System.out.println(jack.getName() + "\t" + jack.printSalary());
System.out.println(marry.getName() + "\t" + marry.printSalary());
System.out.println(jim.getName() + "\t"+ "\t" + jim.printSalary());
System.out.println(lili.getName() + "\t" + lili.printSalary());
System.out.println(sunny.getName() + "\t" + sunny.printSalary());
}
}
class Employee {
private String name;
private double salary;
private int month = 12;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public double printSalary() {
return salary*12;
}
public String getName() {
return name;
}
}
class Worker extends Employee {
public Worker(String name, double salary) {
super(name, salary);
}
}
class Peasant extends Employee {
public Peasant(String name, double salary) {
super(name, salary);
}
}
class Teacher extends Employee {
private double singlesalary;
private int day;
public Teacher(String name, double salary, double singlesalary, int day) {
super(name, salary);
this.singlesalary = singlesalary;
this.day = day;
}
public double printSalary() {
return super.printSalary() + singlesalary * day;
}
}
class Scientist extends Employee {
private double bonus;
public Scientist(String name, double salary, double bonus) {
super(name, salary);
this.bonus = bonus;
}
@Override
public double printSalary() {
return super.printSalary() + bonus;
}
}
class Waiter extends Employee {
public Waiter(String name, double salary) {
super(name, salary);
}
}
结果如下:
(2)老师写的(老师将老师的课时费、科学家的奖金、月份用get、set方法设置更灵活)
代码如下:
package com.hspedu.homework.homework5;
public class Homework05 {
public static void main(String[] args) {
Worker jack = new Worker("jack", 10000);
jack.setSalMonth(15);//灵活额修改带薪月份
jack.printSal();
Peasant smith = new Peasant("smith", 20000);
smith.printSal();
//老师测试
Teacher teacher = new Teacher("顺平", 2000);
//老师有课时费
teacher.setClassDays(360);
teacher.setClassSal(1000);
teacher.printSal();
//科学家
Scientist scientist = new Scientist("钟南山", 20000);
scientist.setBonus(2000000);
scientist.printSal();
}
}
package com.hspedu.homework.homework5;
public class Employee { //父类
//属性
//分析有一个带薪的月份 13 , 15, 12
private String name;
private double sal;
private int salMonth = 12;
//方法
//打印全年工资
public void printSal() {
System.out.println(name + " 年工资是: " + (sal * salMonth));
}
public Employee(String name, double sal) {
this.name = name;
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public int getSalMonth() {
return salMonth;
}
public void setSalMonth(int salMonth) {
this.salMonth = salMonth;
}
}
package com.hspedu.homework.homework5;
public class Worker extends Employee{ //子类
//属性
//工人,农民,服务生只有基本工资 sal
public Worker(String name, double sal) {
super(name, sal);
}
//方法
@Override
public void printSal() {
System.out.print("工人 ");
super.printSal();//使用父类的printSal()
}
}
package com.hspedu.homework.homework5;
public class Peasant extends Employee {//子类
//属性
//农民,服务生只有基本工资 sal
//方法
public Peasant(String name, double sal) {
super(name, sal);
}
//年工资
@Override
public void printSal() {
System.out.print("农民 ");
super.printSal();
}
}
package com.hspedu.homework.homework5;
public class Teacher extends Employee{//子类
//特有属性
private int classDays; //一年上课次数
private double classSal; //课时费
public Teacher(String name, double sal) {
super(name, sal);
}
//方法-重写printSal
@Override
public void printSal() { //老师不能使用super.printSal()
System.out.print("老师 ");
System.out.println(getName() + " 年工资是: "
+ (getSal() * getSalMonth() + classDays * classSal ));
}
public int getClassDays() {
return classDays;
}
public void setClassDays(int classDays) {
this.classDays = classDays;
}
public double getClassSal() {
return classSal;
}
public void setClassSal(double classSal) {
this.classSal = classSal;
}
}
package com.hspedu.homework.homework5;
public class Scientist extends Employee{ //子类
//特有属性
//年终奖 bonus
private double bonus;
//方法
public Scientist(String name, double sal) {
super(name, sal);
}
//重写年工资打印
@Override
public void printSal() {
System.out.print("科学家 ");
System.out.println(getName() + " 年工资是: " + (getSal() * getSalMonth() + bonus));
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
作业6(代码阅读)
注:优先找子类,从下往上找,找到就停,遇到访问受限的也不会跳过会直接报错。故Son
类中的this
只能找到本类的name
和g1
,无法找Grand
类的name
和g1
。
作业7
代码
代码如下:
package com.hspedu.homework;
public class Homework07 {
public static void main(String[] args) {
}
}
class Test{ //父类
String name = "Rose";
Test(){
System.out.println("Test");//(1)
}
Test(String name){//name john
this.name = name;//这里把父类的 name 修改 john
}
}
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 (5) john
System.out.println(this.name);//(4) Jack (6) Jack
}
public static void main(String[] args) {
//老韩分析
//1. new Demo()
new Demo().test(); //匿名对象
new Demo("john").test();//匿名
}
}
结果如下:
作业8
代码
代码如下:
package com.hspedu.homework;
public class Homework08 {
public static void main(String[] args) {
// CheckingAccount checkingAccount = new CheckingAccount(1000);
// checkingAccount.deposit(10);// 1010 - 1 = 1009
// checkingAccount.withdraw(9);//1009 - 9 = 1000 -1= 999
// System.out.println(checkingAccount.getBalance());
//测试SavingsAccount
SavingsAccount savingsAccount = new SavingsAccount(1000);
savingsAccount.deposit(100);
savingsAccount.deposit(100);
savingsAccount.deposit(100);
System.out.println(savingsAccount.getBalance());//1300
savingsAccount.deposit(100);
System.out.println(savingsAccount.getBalance());//1400-1=1399
//月初,定时器自动调用一下 earnMonthlyInterest
savingsAccount.earnMonthlyInterest();//统计利息
System.out.println(savingsAccount.getBalance());//1399 + 13.99 =1412.99
savingsAccount.withdraw(100);//免手续
System.out.println(savingsAccount.getBalance());//1412.99 -100 =1312.99
savingsAccount.withdraw(100);//免手续
savingsAccount.withdraw(100);//免手续
System.out.println(savingsAccount.getBalance());//1412.99 -200 =1112.99
savingsAccount.deposit(100);//扣手续费
System.out.println(savingsAccount.getBalance());//1112.99 + 100 = 1212.99 - 1 = 1211.99
}
}
package com.hspedu.homework;
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;
}
}
package com.hspedu.homework;
/*
在上面类的基础上扩展 新类CheckingAccount对每次存款和取款都收取1美元的手续费
*/
public class CheckingAccount extends BankAccount{//新的账号
//属性
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
@Override
public void deposit(double amount) {//存款
super.deposit(amount - 1);//巧妙的使用了父类的 deposit
//1 块钱转入银行的账号
}
@Override
public void withdraw(double amount) {//取款
super.withdraw(amount + 1);
//1 块钱转入银行的账号
}
}
package com.hspedu.homework;
/*
扩展前一个练习的BankAccount类,
新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),
并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
*/
public class SavingsAccount extends BankAccount {
//老韩分析
//新增加属性
private int count = 3;
private double rate = 0.01;//利率
public void earnMonthlyInterest() {//每个月初,我们统计上个月的利息,同时将count=3
count = 3;//
super.deposit(getBalance() * rate);
}
@Override
public void deposit(double amount) {
//判断是否还可以免手续费
if(count > 0) {
super.deposit(amount);
} else {
super.deposit(amount - 1);//1 块转入银行
}
count--;//减去一次
}
@Override
public void withdraw(double amount) {//取款
//判断是否还可以免手续费
if(count > 0) {
super.withdraw(amount);
} else {
super.withdraw(amount + 1);//1 块转入银行
}
count--;
}
public SavingsAccount(double initialBalance) {
super(initialBalance);
}
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;
}
}
作业9
代码
代码如下:
package com.hspedu.homework8;
public class homework8 {
public static void main(String[] args) {
new LabeledPoint("Black", 1929, 230.07);
}
}
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
class LabeledPoint extends Point {
private String label;
public LabeledPoint(String label, double x, double y) {
super(x, y);
this.label = label;
}
}
作业10
代码
代码如下:
package com.hspedu.homework8;
public class homework8 {
public static void main(String[] args) {
Doctor doctor = new Doctor("jack", 34, "主治医师", "男", 10000);
Doctor doctor1 = new Doctor("marry", 24, "实习医师", "女", 6000);
Doctor doctor2 = new Doctor("marry", 24, "实习医师", "女", 6000);
Doctor doctor3=null;
System.out.println(doctor.equals(doctor1));
System.out.println(doctor1.equals(doctor2));
System.out.println(doctor1.equals(doctor3));//对象为空时返回false
}
}
class Doctor {
private String name;
private int age;
private String job;
private String gender;
private double salary;
public Doctor(String name, int age, String job, String gender, double salary) {
this.name = name;
this.age = age;
this.job = job;
this.gender = gender;
this.salary = 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 getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//重写父类(Object)的equals()方法
@Override
public boolean equals(Object obj) {
//判断两个比较对象是否相同
if(this==obj){
return true;
}
//判断obj 是否是 Doctor类型或其子类
//过关斩将 校验方式
if(!(obj instanceof Doctor)){//不是的话
return false;
}
//向下转型, 因为obj的运行类型是Doctor或者其子类型
return name.equals(((Doctor) obj).name)&&age==((Doctor) obj).age&&
job.equals(((Doctor) obj).job)&&gender.equals(((Doctor) obj).gender)&&
salary==((Doctor) obj).salary;
}
}
结果如下:
注:重写父类(Object)
的equals()
方法,①判断两个比较对象是否相同。②用instanceof
判断obj
是否是 Doctor
类型或其子类。(过关斩将 校验方式)③向下转型, 因为obj
的运行类型是Doctor
或者其子类型。
作业11
作业12
注:equals
默认判断的是地址是否相同。
作业13
代码
(1)自己写的
代码如下:
package com.hspedu.homework13;
public class homework13 {
public static void main(String[] args) {
Student jack = new Student("jack", "男", 13, "00023102");
Student tom = new Student("tom", "男", 16, "00020103");
Teacher lili = new Teacher("lili", "女", 35, 8);
Teacher rose = new Teacher("rose", "女", 33, 6);
Person[] people = {jack, tom, lili, rose};
for (int i = 0; i < people.length; i++) {
System.out.println(people[i].printInfo());
System.out.println("---------------------------------");
}
//按照年龄从高到低排序
Person temp;
for (int i = 0; i < people.length - 1; i++) {
for (int j = 0; j < people.length - 1 - i; j++) {
if (people[j].getAge() < people[j + 1].getAge()) {
temp = people[j];
people[j] = people[j + 1];
people[j + 1] = temp;
}
}
}
System.out.println("按年龄从高到低排序后输出如下");
for (int i = 0; i < people.length; i++) {
System.out.println(people[i].printInfo());
System.out.println("---------------------------------");
}
//调用study方法或teach方法
homework13 homework13 = new homework13();
System.out.println(homework13.callStudy(jack));
System.out.println(homework13.callStudy(rose));
}
//实现调用study方法或teach方法的方法
public String callStudy(Person person) {
if (person instanceof Student) {
return ((Student) person).study();
} else if (person instanceof Teacher) {
return ((Teacher) person).teach();
}
return null;
}
}
class Person {
private String name;
private String sex;
private double age;
public Person(String name, String sex, double age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public double getAge() {
return age;
}
//因Student类和Teacher类都有play方法,
//故放在父类写,子类实现重写
public String play() {
return this.getName() + "爱玩";
}
//打印信息方法
public String printInfo() {
return "姓名:\t" + getName() + "\n" + "年龄:\t" +
getAge() + "\n" + "性别:\t" + sex;
}
}
class Student extends Person {
private String stu_id;
public Student(String name, String sex, double age, String stu_id) {
super(name, sex, age);
this.stu_id = stu_id;
}
public String study() {
return "我承诺,我会好好学习";
}
public String play() {
return super.play() + "足球";
}
@Override
public String printInfo() {
return super.printInfo() + "\n" + "学号:\t" + stu_id + "\n" + study() + "\n" + play();
}
}
class Teacher extends Person {
private double work_age;
public Teacher(String name, String sex, double age, double work_age) {
super(name, sex, age);
this.work_age = work_age;
}
public String teach() {
return "我承诺,我会认真教学。";
}
public String play() {
return super.play() + "象棋";
}
@Override
public String printInfo() {
return super.printInfo() + "\n" + "工龄:\t" + work_age + "\n" + teach() + "\n" + play();
}
}
结果如下:
(2)老师写的
代码如下:
package com.hspedu.homework.homework13;
public class Homework13 {
public static void main(String[] args) {
//测试老师
Teacher teacher = new Teacher("张飞", '男', 30, 5);
teacher.printInfo();
//测试
Student student = new Student("小明", '男', 15, "00023102");
System.out.println("-----------------------------------");
student.printInfo();//封装
//定义多态数组,里面保存2个学生和2个教师,要求按年龄从高到低排序
Person[] persons = new Person[4];
persons[0] = new Student("jack", '男', 10, "0001");
persons[1] = new Student("mary", '女', 20, "0002");
persons[2] = new Teacher("smith", '男', 36, 5);
persons[3] = new Teacher("scott", '男', 26, 1);
//创建对象
Homework13 homework13 = new Homework13();
homework13.bubbleSort(persons);
//输出排序后的数组
System.out.println("---排序后的数组-----");
for(int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);
}
//遍历数组,调用test方法
System.out.println("=======================");
for (int i = 0; i < persons.length; i++) {//遍历多态数组
homework13.test(persons[i]);
}
}
//定义方法,形参为Person类型,功能:调用学生的study或教师的teach方法
//分析这里会使用到向下转型和类型判断
public void test(Person p) {
if(p instanceof Student) {//p 的运行类型如果是Student
((Student) p).study();
} else if(p instanceof Teacher) {
((Teacher) p).teach();
} else {
System.out.println("do nothing...");
}
}
//方法,完成年龄从高到底排序
public void bubbleSort(Person[] persons) {
Person temp = null;
for (int i = 0; i < persons.length - 1; i++) {
for (int j = 0; j < persons.length - 1 - i; j++) {
//判断条件, 注意这里的条件可以根据需要,变化
if(persons[j].getAge() < persons[j+1].getAge()) {
temp = persons[j];
persons[j] = persons[j + 1];
persons[j + 1] = temp;
}
}
}
}
}
package com.hspedu.homework.homework13;
/*
抽取一个父类Person类,将共同属性和方法放到Person类
*/
public class Person {//父类
private String name;
private char gender;
private int age;
//方法
public Person(String name, char gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//编写一个play 方法, 把共有的输出内容写到父类
public String play() {
return name + "爱玩";
}
//返回一个基本信息
/*
姓名:张飞
年龄:30
性别:男
*/
public String basicInfo() {
return "姓名: " + name + "\n年龄: " + age + "\n性别: " + gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender=" + gender +
", age=" + age +
'}';
}
}
package com.hspedu.homework.homework13;
/*
Student类有名称(name),性别(sex),年龄(age),学号(stu_id),
做合理封装,通过构造器在创建对象时将4个属性赋值。
学生需要有学习的方法(study),在方法里写生“我承诺,我会好好学习。”
*/
public class Student extends Person{ //
//属性
private String stu_id;
//方法
public Student(String name, char gender, int age, String stu_id) {
super(name, gender, age);
this.stu_id = stu_id;
}
public String getStu_id() {
return stu_id;
}
public void setStu_id(String stu_id) {
this.stu_id = stu_id;
}
//我承诺,我会好好学习
public void study() {
System.out.println(getName() + "承诺,我会好好学习 老韩讲的 java");
}
/**
* 学生爱玩足球
* @return
*/
@Override
public String play() {
return super.play() + "足球";
}
//编写一个输出信息的方法,这样体现封装
public void printInfo() {
System.out.println("学生的信息:");
System.out.println(super.basicInfo());
System.out.println("学号: " + stu_id);
study();//组合, 变化万千
System.out.println(play());
}
@Override
public String toString() {
return "Student{" +
"stu_id='" + stu_id + '\'' +
'}' + super.toString();
}
}
package com.hspedu.homework.homework13;
/*
写一个Teacher类,Teacher类有名称(name),性别(sex),年龄(age),工龄(work_age),
做合理封装,通过构造器在创建对象时将4个属性赋值
*/
public class Teacher extends Person {
//属性
private int work_age;
//方法
public Teacher(String name, char gender, int age, int work_age) {
super(name, gender, age);
this.work_age = work_age;
}
public int getWork_age() {
return work_age;
}
public void setWork_age(int work_age) {
this.work_age = work_age;
}
//教师需要有教学的方法(teach),在方法里写上“我承诺,我会认真教学。
public void teach() {
System.out.println(getName() + "承诺,我会认真教学 java...");
}
/**
* 老师爱玩象棋
*/
@Override
public String play() {
return super.play() + "象棋";
}
//输出信息方法
public void printInfo() {
System.out.println("老师的信息:");
System.out.println(super.basicInfo());
System.out.println("工龄: " + work_age);
teach();
System.out.println(play());
}
@Override
public String toString() {
return "Teacher{" +
"work_age=" + work_age +
'}' + super.toString();
}
}
作业14(程序阅读题)
分析
注:在B
类有参构造其中,隐含了一个A
类的无参构造器,即super()
。
作业15
代码
代码如下:
package com.hspedu.homework;
public class Homework15 {
public static void main(String[] args) {
AAA obj = new BBB();//向上转型
AAA b1 = obj;
System.out.println("obj的运行类型=" + obj.getClass());//BBB
obj = new CCC();//向上转型
System.out.println("obj的运行类型=" + obj.getClass());//CCC
obj = b1;
System.out.println("obj的运行类型=" + obj.getClass());//BBB
}
}
class AAA {//超类
}
class BBB extends AAA {//父类
}
class CCC extends BBB {//子类
}
结果如下:
注:编译类型无法修改!!上示举例说明了运行类型可以随时变化!!