1.定义一个Student类,并要求其他类在使用Student类的时候,最多只能创建10个Student类的对象,如何实现?
提示: 首先,要实现该功能,就不能让外部类直接使用
new Student(…)的方式来创建对象,如何不能让其他类new Student(…),
只需将Student类的所有构造方法的,权限改为private即可。
接着,把创建对Student对象的工作,交给一个专门的方法去做(想想这个方法应该是怎样的方法)。
代码:
public class Exercise01 {
public static void main(String[] args) {
for (int i = 0;i < 20;i++){//假设要创建20个Student的对象
String name = "小明" + i + 1;
int sno = i + 1;
Student stu = Student.newStudent(name, sno);
if (stu == null){
break;
}
}
}
}
class Student{
String name;
int sno;
private Student(String name, int sno){//2参构造方法
this.name = name;
this.sno = sno;
}
static int count = 0;//计数创建对象的个数
//定义静态成员方法,来创建对象,可直接通过类名来调用
public static Student newStudent(String name, int sno){
count++;
if(count <= 10){//数量小于10时,可成功创建对象
System.out.println("成功创建学生对象" + count);
return new Student(name,sno);
}
else{//数量大于10时,不允许创建对象
System.out.println("创建失败,创建数量已达上限!");
return null;
}
}
}
运行结果:
2.将人,学生和老师(老师多了教工编号和讲课的行为)案例用继承实现,并添加军人类,军人的特殊属性假设有部队编号,军人特有的行为假设有训练。
代码:
public class Inherit {
public static void main(String[] args) {
//创建学生对象
Students stu = new Students("小红", 1);
System.out.print("学生:");
stu.print();
System.out.println("学生的行为有:");
stu.eat();
stu.sleep();
stu.study();
//创建教师对象
Teacher teacher = new Teacher("张老师", 2);
System.out.print("老师:");
teacher.print();
System.out.println("老师的行为有:");
teacher.eat();
teacher.sleep();
teacher.teach();
//创建军人对象
Soldier soldier = new Soldier("小明", 3);
System.out.print("军人:");
soldier.print();
System.out.println("军人的行为有:");
soldier.eat();
soldier.sleep();
soldier.train();
}
}
//定义人这个类
class Human{
//姓名
String name;
//构造方法
public Human(){
}
public Human(String name){
this.name = name;
}
//吃饭
public void eat() {
System.out.println("eating");
}
//睡觉
public void sleep() {
System.out.println("sleeping");
}
//输出信息
public void print(){
System.out.println("name = " + this.name);
}
}
//定义学生类
class Students extends Human{
//学号
int sno;
//构造方法
public Students(String name, int sno){
this.name = name;
this.sno = sno;
}
//学习
public void study(){
System.out.println("studying");
}
//输出信息
public void print(){
System.out.println("name = " + this.name + ", sno = " + this.sno);
}
}
//定义教师类
class Teacher extends Human{
//教工编号
int tno;
//构造方法
public Teacher(String name, int tno){
this.name = name;
this.tno = tno;
}
//讲课
public void teach(){
System.out.println("teaching");
}
//输出信息
public void print(){
System.out.println("name = " + this.name + ", tno = " + this.tno);
}
}
//定义军人类
class Soldier extends Human{
//部队编号
int troopNo;
//构造方法
public Soldier(String name, int troopNoo){
this.name = name;
this.troopNo = troopNo;
}
//训练
public void train(){
System.out.println("training");
}
//输出信息
public void print(){
System.out.println("name = " + this.name + ", troopNo = " + this.troopNo);
}
}
运行结果:
3.结合多态发生的条件,及继承相关知识,自己总结并验证下,哪些方法无法实现多态效果。
解答:
考虑到多态发生的条件是继承、方法覆盖、父类引用指向子类实例。
所以哪些方法(行为),实现不了多态效果呢?
a. 父类中的private方法(不能被子类覆盖)
b. 父类中的构造方法(不能被子类继承)
c. 父类中的静态方法(不能被子类覆盖)
d. 父类中被final修饰的方法(不能被子类覆盖)
4.自己定义一个类,类中定义3个成员变量,这3个成员变量都被final修饰,请用3种不同方式,为这3个被final修饰的成员变量赋值。
代码:
public class FinalExercise {
public static void main(String[] args) {
Student student = new Student(18);
System.out.println("NO = " + student.NO + ",NAME = " + student.NAME + ",AGE = " + student.AGE);
}
}
class Student{
//1.通过成员变量初始化的方式给被final修饰的成员变量赋值
final int NO = 1;
final String NAME;
final int AGE;
//2.通过构造代码块给被final修饰的成员变量赋值
{
this.NAME = "李蕾";
}
//3.通过构造方法给被final修饰的成员变量赋值
public Student(int AGE){
this.AGE = AGE;
}
}
运行结果:
5.实现如下多态案例,分别定义Person类,SouthPerson(表示南方人),NorthPerson(表示北方人)
Person
eat()
SouthPerson
eat()
NorthPerson
eat()
写代码实现,eat方法的多态效果
1:人要吃饭
2:南方人吃炒菜和米饭
3:北方人吃烩菜和馒头
代码:
public class PolymorphismExercise {
public static void main(String[] args) {
Person p = new Person();
p.eat();
//父类Person引用指向SouthPerson子类实例
p = new SouthPerson();
p.eat();
//父类Person引用指向NorthPerson子类实例
p = new NorthPerson();
p.eat();
}
}
//定义Person类
class Person{
String name;
public void eat(){
System.out.println("人要吃饭");
}
}
//定义SouthPerson类(表示南方人)
class SouthPerson extends Person{
@Override
public void eat(){
System.out.println("南方人吃炒菜和米饭");
}
}
//定义NorthPerson类(表示北方人)
class NorthPerson extends Person{
@Override
public void eat(){
System.out.println("北方人吃烩菜和馒头");
}
}
运行结果:
#个人学习记录,如发现有错误之处,欢迎与我交流