题目16
/* 定义一个交通工具类,并定义抽象方法,说明交通工具可以移动。继承交通工具而产生汽车、飞机、轮船类,并实现其移动方法。 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-28 22:10 */ public class Exer7 { /* 定义一个交通工具类,并定义抽象方法,说明交通工具可以移动。继承交通工具而产生汽车、飞机、轮船类,并实现其移动方法。 */ public static void main(String[] args) { Transportation transportation = new Transportation(); transportation.abstractMethod(); Car2 car2 = new Car2(); car2.abstractMethod(); Airplane airplane = new Airplane(); airplane.abstractMethod(); Boat boat = new Boat(); boat.abstractMethod(); } } class Transportation { public void abstractMethod() { System.out.println("交通工具可以移动"); } } class Car2 { public void abstractMethod() { System.out.println("汽车地上跑"); } } class Airplane { public void abstractMethod() { System.out.println("飞机天上飞"); } } class Boat { public void abstractMethod() { System.out.println("轮船水中游"); } }
题目17
/* equals()编程1: (1)编写一个人类Person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。(10分) (2)再编写一个学生类Student,它继承Person类,其中包含学号属性,包含构造方法以及显示学号的方法。(10分) (3)在Student类里面重写equals方法,只要是学号和姓名一致就认为是同一个学生。(20分) (4)最后编写一个测试类,此类里面包含main()方法,在main()方法中定义两个学生s1和s2并给他们赋值,最后显示他们的学号、姓名、性别以及年龄,以及判断两个学生是否是同一个学生。(10分) */
package com.jerry.exer; import java.util.Locale; /** * @author jerry_jy * @create 2022-09-28 22:15 */ public class Exer8 { public static void main(String[] args) { Person2 person1 = new Person2("Zhang3", 'M', 20); Person2 person2 = new Person2("Li4", 'F', 21); System.out.println(person1.toString()); Student student1 = new Student(person1, 1001); System.out.println(student1.toString()); Student student2 = new Student(person2, 1002); System.out.println(student2.equals(student1)); } } class Person2 { String name; char gender; int age; public Person2() { } public Person2(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; } @Override public String toString() { return "Person2{" + "name='" + name + '\'' + ", gender=" + gender + ", age=" + age + '}'; } } class Student extends Person2 { int stuID; public Student() { } public Student(Person2 person2, int stuID) { super(person2.getName(), person2.getGender(), person2.getAge()); this.stuID = stuID; } public int getStuID() { return stuID; } public void setStuID(int stuID) { this.stuID = stuID; } public boolean equals(Student student) { if (this.getStuID() == student.getStuID() && this.getName() == student.getName()) { return true; } else { return false; } } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", gender=" + gender + ", age=" + age + ", stuID=" + stuID + '}'; } }
题目18
/* 按要求实现下列问题: 1)封装一个西游记人物类Person:包含私有属性:姓名(name)、身高(height)、武器(weapon)、种族(race),并提供访问器(get方法)和修改器(set方法) 2) 在测试类中实例化三个对象:tangseng(唐僧)、sunwukong(孙悟空)、baigujing(白骨精),分别设置他们的种族为:人族、仙族、妖族 3) 在打印对象时,按照例如:“姓名:孙悟空;种族:仙族”的格式在控制台输出信息。 4) 提供一个技能方法void skill(),这个方法根据当前对象name属性判断技能,如果是孙悟空,就打印“七十二变”,如果是唐僧,就打印“紧箍咒”,如果是白骨精,就打印“九阴白骨爪”,在测试类中调用这个方法。 5) 重写equals方法进行判断,如果种族属性是人族或者仙族,则返回true,如果是妖族,则返回false,在测试类中对孙悟空和唐僧进行比较,打印结果,对唐僧和白骨金进行比较,并打印结果。 */
package com.jerry.exer; import sun.security.provider.Sun; import java.util.Objects; /** * @author jerry_jy * @create 2022-09-28 22:24 */ public class Exer9 { public static void main(String[] args) { Person3 tangseng = new Tang(); tangseng.setName("唐僧"); tangseng.setRace("人族"); tangseng.info(); tangseng.skill(); Person3 sunwukong = new SunWuKong(); sunwukong.setName("孙悟空"); sunwukong.setRace("仙族"); sunwukong.info(); sunwukong.skill(); System.out.println(sunwukong.equals(tangseng)); Person3 baigujing = new Bai(); baigujing.setName("白骨精"); baigujing.setRace("妖族"); baigujing.info(); baigujing.skill(); System.out.println(baigujing.equals(tangseng)); } } class Person3 { String name; double height; String weapon; String race; public Person3() { } public Person3(String name, double height, String weapon, String race) { this.name = name; this.height = height; this.weapon = weapon; this.race = race; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getWeapon() { return weapon; } public void setWeapon(String weapon) { this.weapon = weapon; } public String getRace() { return race; } public void setRace(String race) { this.race = race; } public void info() { System.out.println("姓名:" + name + ",种族:" + race); } public void skill() { } public boolean equals(Person3 person3) { if ("人族".equals(this.getRace())||"仙族".equals(person3.getRace())){ return true; }else { return false; } } } class Tang extends Person3 { public void skill() { System.out.println("紧箍咒"); } } class SunWuKong extends Person3 { public void skill() { System.out.println("七十二变"); } } class Bai extends Person3 { public void skill() { System.out.println("九阴白骨爪"); } }
题目19
写出程序结果
class Test { Test() { System.out.println("Test"); } } class Demo extends Test { Demo() { // super(); System.out.println("Demo"); } public static void main(String[] args) { new Demo(); new Test(); } }
题目20
写出程序结果
class Super { int i = 0; public Super(String a) { System.out.println("A"); i = 1; } public Super() { System.out.println("B"); i += 2; } } class Demo extends Super { public Demo(String a) { // super(); System.out.println("C"); i = 5; } public static void main(String[] args) { int i = 4; Super d = new Demo("A"); System.out.println(d.i); } }
题目21
写出程序结果
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-29 9:36 */ /* 写出程序结果: B C 5 */ public class Exer11 extends Super{ public Exer11(String a) { // super(); System.out.println("C");//C i = 5; } public static void main(String[] args) { int i = 4; Super d = new Exer11("A"); System.out.println(d.i);//5 } } class Super { int i = 0; public Super(String a) { System.out.println("A"); i = 1; } public Super() { System.out.println("B");//B i += 2; } }
题目22
写出程序结果
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-29 9:46 */ /* 写出程序结果: */ //编译失败,因为父类中缺少空参数的构造器。 //或者子类应该通过super语句指定要调用的父类中的构造器。 public class Exer12 extends Super1{ public Exer12(String s) { i = 2; } public static void main(String[] args) { Exer12 d = new Exer12("yes"); System.out.println(d.i); } } class Super1 { int i = 0; public Super1(String s) { i = 1; } }
题目23
/*
编程题
编写一个Person类,包括属性(name、age),构造器、方法say(返回自我介绍的字符串)。
编写一个Student类,继承Person类,增加ID、score属性,以及构造器,并覆盖say方法。
编写StudentTest类,创建分别Person和Student对象,调用say方法输出自我介绍。
*/
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-29 9:56 */ public class Exer13 { /* 编程题 1. 编写一个Person类,包括属性(name、age),构造器、方法say(返回自我介绍的字符串)。 2. 编写一个Student类,继承Person类,增加ID、score属性,以及构造器,并覆盖say方法。 3. 编写StudentTest类,创建分别Person和Student对象,调用say方法输出自我介绍。 */ public static void main(String[] args) { Person4 person4 = new Person4("Zhang3", 20); person4.say(); Student1 student1 = new Student1(person4, 1001, 99); student1.say(); } } class Person4 { String name; int age; public Person4() { } public Person4(String name, int age) { this.name = name; this.age = age; } public void say() { System.out.println("我叫:" + name + ",今年:" + age + "岁"); } } class Student1 extends Person4 { int ID; int score; public Student1() { } public Student1(Person4 person4, int ID, int score) { super(person4.name, person4.age); this.ID = ID; this.score = score; } public void say() { System.out.println("我叫:" + name + ",今年:" + age + "岁" + ",学号:" + ID + ",分数:" + score); } }
题目24
写出程序结果
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-29 10:11 */ class A1 { private int a; public void setA(int a) { this.a = a; } public int getA() { return a; } } class B1 extends A1 { private int a; public void setA(int a) { this.a = a; } // public int getA(){ // return a; // } } public class PersonTest { public static void main(String[] args) { A1 c = new B1(); c.setA(5); System.out.println(c.getA());//0 } }