day12作业
一,选择题
1.C 2.B 3.D 4.AC 5.AB 6.AD 7.AB 8.BC
二 ,判断题
1.√ 2.√ 3.× 4.√ 5.√ 6.× 7.√ 8.√ 9.× 10.√ 11.×
三,简答题
1.
final是一个修饰符表示最后的、最终的,用final修饰的类不能被继承,用final修饰的变量变成常量且只能被赋值一次,final修饰的方法不能被重写。
abstract是一个修饰符,可以修饰方法和类,当多个类存在相同的功能,但是功能的主体不同,可以定义抽象方法。抽象类不一定有抽象方法,有抽象方法的类一定是抽象类或接口。
抽象类不能实例化,抽象类的子类要么是抽象类,要么重写抽象类中的所有抽象方法。
2.
抽象类的成员变量可以是变量也可以是常量,接口的成员变量只能是常量;抽象类有构造方法,接口没有构造方法;抽象类的成员方法可以是抽象的也可以是非抽象的,接口的构造方法只能是抽象的。在设计理念上,被继承体现的是“is a”的关系,抽象类中定义的是该继承体系的共性功能;被实现体现的是“like a”关系,接口中定义的是该继承体系的扩展功能。
3.成员内部类,静态内部类,局部内部类,匿名内部类
静态内部类是指被声明为static的内部类,不能访问外部类的普通成员变量,只能访问外部类中的静态成员变量和静态方法。
去掉static关键字的就是成员内部类,可以自由引用外部类的属性和方法。
局部内部类是指定义在一个代码块内的类,作用范围为其所在的代码块,局部类类似于局部变量,不能用public,private,static和protected修饰,只能访问方法中定义为final的局部变量。
匿名内部类是一种没有类名的内部类,不使用关键字class,extends,implements,它必须继承其他类或实现其他接口。
4.垃圾回收是java中自动管理内存的另一种叫法,垃圾回收的目的是为了程序保尽可能多的可用堆,JVM会删除堆上不再需要从堆引用的对象。
class Test7_Animal { public static void main(String[] args) { Rabbit r = new Rabbit("美人","黑色","哺乳类"); System.out.println("那只" + r.getColor() + "的,名叫" + r.getName() + "的兔子正在" + r.voice()); System.out.println("兔子是" + r.getCategory() + "," + r.eat()); System.out.println("-----------------------------"); Frog f = new Frog("大兵","绿色","非哺乳类"); System.out.println("那只" + f.getColor() + "的,名叫" + f.getName() + "的青蛙正在" + f.voice()); System.out.println("青蛙是" + f.getCategory() + "," + f.eat()); f.swim(); } } abstract class Animal { private String name; private String color; private String category; public Animal() {} public Animal(String name,String color,String category) { this.name = name; this.color = color; this.category = category; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setColor(String color) { this.color = color; } public String getColor() { return color; } public void setCategory(String category) { this.category = category; } public String getCategory() { return category; } public abstract void eat(); public abstract void voice(); } interface Swimming { public abstract void swim(); } class Rabbit extends Animal { public Rabbit() {} public Rabbit(String name,String color,String category) { super(name,color,category); } public void eat() { System.out.println("爱吃胡萝卜"); } public void voice() { System.out.println("叽叽的叫"); } } class Frog extends Animal implements Swimming { public Frog() {} public Frog(String name,String color,String category) { super(name,color,category); } public void eat() { System.out.println("爱吃昆虫"); } public void voice() { System.out.println("呱呱的叫"); } public void swim() { System.out.println("虽然不是鱼,但青蛙也是泳坛高手"); } }
class Test8_Star { public static void main(String[] args) { Star s = new Star("马素素"); System.out.println("大家好!我是" + s.getName()); s.film(); s.teleplay(); s.sing(); } } interface Film { public abstract void film(); } interface Teleplay { public abstract void teleplay(); } interface Sing { public abstract void sing(); } class Star implements Film,Teleplay,Sing{ private String name; public Star() {} public Star(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void film() { System.out.println("我能演电影"); } public void teleplay() { System.out.println("我能演电视剧"); } public void sing() { System.out.println("我会唱歌"); } }
class Test9_Pci { public static void main(String[] args) { VideaCard v = new VideaCard(10); v.send(); AudioCard a = new AudioCard(20); a.send(); NetCard n = new NetCard(30); n.send(); } } interface Pci { public abstract void send(); } class VideaCard implements Pci{ private int datav; public VideaCard() {} public VideaCard(int datav) { this.datav = datav; } public void setDatav(int datav) { this.datav = datav; } public int getDatav() { return datav; } public void send() { System.out.println(datav); } } class AudioCard implements Pci{ private int dataa; public AudioCard() {} public AudioCard(int dataa) { this.dataa = dataa; } public void setDataa(int dataa) { this.dataa = dataa; } public int getDataa() { return dataa; } public void send() { System.out.println(dataa); } } class NetCard implements Pci{ private int datan; public NetCard() {} public NetCard(int datan) { this.datan = datan; } public void setDatan(int datan) { this.datan = datan; } public int getDatan() { return datan; } public void send() { System.out.println(datan); } }
class Test10_Sort { public static void main(String[] args) { Student s1 = new Student("1号","小明",22,95); Student s2 = new Student("2号","小红",21,90); if (Double.compare(s1.getGoal(),s2.getGoal()) == 1) { System.out.println(s1.getGoal() + "..." + s2.getGoal()); }else if (Double.compare(s1.getGoal(),s2.getGoal()) == 0) { System.out.println(s1.getGoal() + "..." + s2.getGoal()); }else if (Double.compare(s1.getGoal(),s2.getGoal()) == -1) { System.out.println(s2.getGoal() + "..." + s2.getGoal()); } News n1 = new News(10,"人民日报","4635",16461); News n2 = new News(20,"扬子晚报","18776",68735); if (Double.compare(n1.getNum(),n2.getNum()) == 1) { System.out.println(n2.getNum() + "..." + n1.getNum()); }else if (Double.compare(n1.getNum(),n2.getNum()) == 0) { System.out.println(n2.getNum() + "..." + n1.getNum()); }else if (Double.compare(n1.getNum(),n2.getNum()) == -1) { System.out.println(n1.getNum() + "..." + n2.getNum()); } } } interface Comparable { int compareTo(Object obj); } class Student { private String id; private String name; private int age; private int goal; public Student() {} public Student(String id,String name,int age,int goal) { this.id = id; this.name = name; this.age = age; this.goal = goal; } public void setId(String id) { this.id = id; } public String getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setGoal(int goal) { this.goal = goal; } public int getGoal() { return goal; } public int compareTo(Object obj) { Student other = (Student) obj; return Double.compare(goal,other.goal); } } class News { private int num; private String title; private String content; private int clicks; public News() {} public News(int num,String title,String content,int clicks) { this.num = num; this.title = title; this.content = content; this.clicks = clicks; } public void setNum(int num) { this.num = num; } public int getNum() { return num; } public void setTitle(String title) { this.num = num; } public String getTitle() { return title; } public void setContent(String content) { this.content = content; } public String getContent() { return content; } public void setClicks(int clicks) { this.clicks = clicks; } public int getClicks() { return clicks; } public int compareTo(Object obj) { News other = (News) obj; return Double.compare(num,other.num); } }