一、定义数组工具类
需求:在实际开发中,经常会遇到一些数组使用的工具类。请按照如下要求编写一个数组的工具类:ArrayUtil。
提供一个工具类方法printArr,用于返回整数数组的内容。 提供一个工具类方法getAerage,用于返回平均值(只考虑浮点型数组,且只考虑一维数组)。 定义一个测试类TestDemo,调用该工具类的工具方法,并返回结果。
a. 工具类
package day12. StaticDemo2 ;
public class ArrayUtil {
private ArrayUtil ( ) { } ;
public static String printArr ( int [ ] arr) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( "[" ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( i == arr. length - 1 ) {
sb. append ( arr[ i] ) ;
} else {
sb. append ( arr[ i] ) . append ( ", " ) ;
}
}
sb. append ( "]" ) ;
return sb. toString ( ) ;
}
public static double getAverage ( double [ ] arr) {
double sum = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
sum = sum + arr[ i] ;
}
return sum / arr. length;
}
}
b. 测试类
package day12. StaticDemo2 ;
public class TestDemo {
public static void main ( String [ ] args) {
int [ ] arr1 = { 1 , 2 , 3 , 4 , 5 } ;
String str = ArrayUtil . printArr ( arr1) ;
System . out. println ( str) ;
double [ ] arr2 = { 1.5 , 3.7 , 4.9 , 5.8 , 6.6 } ;
double average = ArrayUtil . getAverage ( arr2) ;
System . out. println ( average) ;
}
}
c. 输出结果
二、定义学生工具类
需求:定义一个集合,用于存储3个学生对象。 学生类的属性为:name、age、gender。 定义一个工具类,用于获取集合中最大学生的年龄。
a. 学生类
package day12. StaticDemo3 ;
public class Student {
private String name;
private int age;
private String gender;
public Student ( ) {
}
public Student ( String name, int age, String gender) {
this . name = name;
this . age = age;
this . gender = gender;
}
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 getGender ( ) {
return gender;
}
public void setGender ( String gender) {
this . gender = gender;
}
public String toString ( ) {
return "Student{name = " + name + ", age = " + age + ", gender = " + gender + "}" ;
}
}
b. 工具类
import java. util. ArrayList ;
public class StudentUtil {
private StudentUtil ( ) { }
public static int getMaxAgeStudent ( ArrayList < Student > list) {
int max = list. get ( 0 ) . getAge ( ) ;
for ( int i = 1 ; i < list. size ( ) ; i++ ) {
int tempAge = list. get ( i) . getAge ( ) ;
if ( tempAge > max) {
max = tempAge;
}
}
return max;
}
}
c. 测试类
package day12. StaticDemo3 ;
import java. util. ArrayList ;
public class Test {
public static void main ( String [ ] args) {
ArrayList < Student > list = new ArrayList < > ( ) ;
Student stu1 = new Student ( "zhangsan" , 12 , "nan" ) ;
Student stu2 = new Student ( "liwi" , 11 , "nan" ) ;
Student stu3 = new Student ( "wangwu" , 10 , "nan" ) ;
list. add ( stu1) ;
list. add ( stu2) ;
list. add ( stu3) ;
int maxAgeStudent = StudentUtil . getMaxAgeStudent ( list) ;
System . out. println ( maxAgeStudent) ;
}
}
d. 输出结果
三、继承和多态综合练习
需求:
定义狗类
属性:年龄,颜色 行为:eat(String something)(something表示吃的东西)、 lookHome方法(无参数) 定义猫类
属性:年龄,颜色 行为:eat(String something)方法(something表示吃的东西)、逮老鼠catchMouse方法(无参数) 定义Person类/饲养员
属性:姓名,年龄 行为:keepPet(Dog dog,String something)方法。功能:喂养宠物狗,something表示喂养的东西 行为:keepPet(Cat cat,String something)方法。功能:喂养宠物猫,something表示喂养的东西 生成空参有参构造,set和get方法 定义测试类(完成以下打印效果):
keepPet(Dog dog,String somethind)方法打印内容如下: 年龄为30岁的老王养了一只黑颜色的2岁的狗 2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃 keepPet(Cat cat,String somethind)方法打印内容如下: 年龄为25岁的老李养了一只灰颜色的3岁的猫 3岁的灰颜色的猫眯着眼睛侧着头吃鱼 思考:
Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化后的好处? Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法?
a. 动物类
package day13. polymorphismDemo2 ;
public class Animal {
private int age;
private String color;
public Animal ( ) {
}
public Animal ( int age, String color) {
this . age = age;
this . color = color;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getColor ( ) {
return color;
}
public void setColor ( String color) {
this . color = color;
}
public void eat ( String something) {
System . out. println ( "动物在吃" + something) ;
}
}
b. 饲养员类
package day13. polymorphismDemo2 ;
public class Person {
private String name;
private int age;
public Person ( ) {
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
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 void keepPet ( Animal animal, String something) {
String p;
if ( animal instanceof Dog ) {
p = "狗" ;
} else if ( animal instanceof Cat ) {
p = "猫" ;
} else {
System . out. println ( "没有这个动物" ) ;
return ;
}
System . out. println ( "年龄为" + this . age + "岁的" + this . name + "养了一只" + animal. getColor ( ) + "颜色的" + animal. getAge ( ) + "岁的" + p) ;
animal. eat ( something) ;
}
}
c. 狗类
package day13. polymorphismDemo2 ;
public class Dog extends Animal {
public Dog ( ) {
}
public Dog ( int age, String color) {
super ( age, color) ;
}
@Override
public void eat ( String something) {
System . out. println ( getAge ( ) + "岁的" + getColor ( ) + "颜色的狗两只前腿死死的抱住" + something + "猛吃" ) ;
}
public void lookHome ( ) {
System . out. println ( "狗在看家" ) ;
}
}
d. 猫类
package day13. polymorphismDemo2 ;
public class Cat extends Animal {
public Cat ( ) {
}
public Cat ( int age, String color) {
super ( age, color) ;
}
@Override
public void eat ( String something) {
System . out. println ( getAge ( ) + "岁的" + getColor ( ) + "颜色的猫眯着眼睛侧着头吃" + something) ;
}
public void catchMouse ( ) {
System . out. println ( "猫抓老鼠" ) ;
}
}
e. 测试类
package day13. polymorphismDemo2 ;
public class Test {
public static void main ( String [ ] args) {
Person wang = new Person ( "老王" , 12 ) ;
Dog dog = new Dog ( 2 , "黑" ) ;
dog. lookHome ( ) ;
wang. keepPet ( dog, "骨头" ) ;
Person li = new Person ( "老李" , 10 ) ;
Cat cat = new Cat ( 1 , "五彩斑斓" ) ;
cat. catchMouse ( ) ;
li. keepPet ( cat, "鱼竿" ) ;
}
}
f. 输出结果
四、抽象类和接口综合练习
需求:将人员设计成标准JavaBean类:
乒乓球运动员:姓名、年龄、学打乒乓球、说英语; 篮球运动员:姓名、年龄、学打篮球; 乒乓球教练:姓名、年龄、教打乒乓球、说英语; 篮球教练:姓名、年龄、教打篮球。
a. 人抽象类
package day14. ainterface. demo3 ;
public abstract class Person {
private String name;
private int age;
public Person ( ) {
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
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 toString ( ) {
return "Person{name = " + name + ", age = " + age + "}" ;
}
}
b. 运动员抽象类
package day14. ainterface. demo3 ;
public abstract class Sporter extends Person {
public Sporter ( ) {
}
public Sporter ( String name, int age) {
super ( name, age) ;
}
public abstract void learn ( ) ;
}
c. 教练抽象类
package day14. ainterface. demo3 ;
public abstract class Teacher extends Person {
public Teacher ( ) {
}
public Teacher ( String name, int age) {
super ( name, age) ;
}
public abstract void teach ( ) ;
}
d. 说英语接口
package day14. ainterface. demo3 ;
public interface SpeakEnglish {
public abstract void speakEnglish ( ) ;
}
e. 乒乓球运动员实现类
package day14. ainterface. demo3 ;
public class PingPangTeacher extends Teacher implements SpeakEnglish {
public PingPangTeacher ( ) {
}
public PingPangTeacher ( String name, int age) {
super ( name, age) ;
}
@Override
public void speakEnglish ( ) {
System . out. println ( "乒乓球教练说英语" ) ;
}
@Override
public void teach ( ) {
System . out. println ( "教乒乓球" ) ;
}
}
f. 乒乓球教练实现类
package day14. ainterface. demo3 ;
public class PingPangSporter extends Sporter implements SpeakEnglish {
public PingPangSporter ( ) {
}
public PingPangSporter ( String name, int age) {
super ( name, age) ;
}
@Override
public void speakEnglish ( ) {
System . out. println ( "乒乓球运动员说英语" ) ;
}
@Override
public void learn ( ) {
System . out. println ( "学乒乓球" ) ;
}
}
g. 篮球运动员类
package day14. ainterface. demo3 ;
public class BasketBallSporter extends Sporter {
public BasketBallSporter ( ) {
}
public BasketBallSporter ( String name, int age) {
super ( name, age) ;
}
@Override
public void learn ( ) {
System . out. println ( "学习打篮球" ) ;
}
}
h. 篮球教练类
package day14. ainterface. demo3 ;
public class BasketBallTeacher extends Teacher {
public BasketBallTeacher ( ) {
}
public BasketBallTeacher ( String name, int age) {
super ( name, age) ;
}
@Override
public void teach ( ) {
System . out. println ( "教打篮球" ) ;
}
}
i. 测试类
package day14. ainterface. demo3 ;
public class Test {
public static void main ( String [ ] args) {
PingPangSporter pingPangSporter = new PingPangSporter ( "666" , 12 ) ;
pingPangSporter. learn ( ) ;
pingPangSporter. speakEnglish ( ) ;
}
}
j. 输出结果