A.抽象类可以有构造方法,接口没有构造方法
B.抽象类可以有属性,接口没有属性
C.抽象类可以有非抽象方法,接口中都是抽象方法????1.8之后
D.抽象类和接口都不能单独创建对象????????抽象类有构造方法 ?可以声明引用但不能创建对象, ?接口无构造方法,但是也可以声明引用
A.一个Java 类只能实现一个接口
B.一个Java 类不能同时继承一个类和实现一个接口
C.一个Java 类可以实现多个接口
D.一个Java 类只能直接继承一个类
A. 接口不能创建对象,可以声明引用
B. 接口中可以定义构造方法
C. 接口中的属性都是公开、静态常量
D. 接口中的方法都是公开、抽象方法
抽象类是类(abstract class) 有构造方法?需要子类继承 extends 里边可以有抽象方法也可以有非抽象的方法,单继承
接口(interface)无构造方法 可以说是都是抽象方法的抽象类 ,全是公有的抽象方法, 需要实现,implements ??实现可以多实现 ??
interface IA{
void m1();
void m2();
}
//写出实现类,类名为MyClass
class MyClass implements IA{
public void m1(){}
public void m2(){}
}
interface IA{
void m1();
void m2();
???? }
???? interface IB extends IA{
void m3();
???? }
class MyClass implements IB{
public void m1(){}
public void m2(){}
public void m3(){}
public void m4(){}
}
???? 在主函数中 IA ia=new MyClass();以ia可以调用方法是()
A. m1 ????B. m2 ????C. m3??????D. m4
A. 类只允许单继承
B. 一个类只能实现一个接口
C. 一个类不能同时继承一个类和实现多个接口
D. 接口只允许单一继承。
System.out.println(ia.a);
abstract ??????public void m2(){}
如果有一个类ClassE 实现ID 接口,如果不希望 ClassE 是抽象的,则需要实现哪些方法?
ma() ??mb() ??mc() ???md()
将以下代码补充完整:
ia.ma();
IB ib=(IB)ic;
ib.mb();
ic.mc();
ID id=(ID)ic;
id.md();
写出以下程序输出的结果:
??????????????false
???true
???false
???true
true true true true
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shone in Green
TeacherA ??teach ??Java
TeacherB ??teach ??Java
需要一个实现ServiceInterface 接口的类MyService。
①第一种方式可以让 MyService 实现ServiceInterface 接口,即: class MyService implements ServiceInterface
②第二种方式可以让 MyService 继承AbstractService 类,即: class MyService extends AbstractService
请问:这两种方式有什么区别?AbstractService 类有什么作用?
class MyService ?implements ServiceInterface{
??????????public ?void doService1(){}
public void doService2(){}
public void doService3(){}
???????????} ?
???????????一种是实体类直接继承接口,需要实现接口中所有的方法,比较麻烦,且接口中可能会有实体类不需要的抽象方法,如果实体类实现接口,就必须实方法
?????????一种是缺省适配器模式由抽象类实现接口,抽象类中可以有抽象和非抽象方法,起到一个过滤子类不需要的方法的作用,把子类不需要的方法实例化,子类再继承抽象类就不需要 重写无用方法了
Animal []as={new Cat,new Dog,new Wolf};
int count=0;
for(int i=0;i<as.length;i++){
?if(as[i] instanceof Dog){
as[i].eat();
as[i].play();
count++;
}else if(as[i] instanceof ?Cat){
as[i].eat();
as[i].play();
count++
}else{
as[i].eat
count++
}
}
System.out.println(count);
“long fact(int?m);”方法的功能为:求参数的阶乘
“long intPower(int m , int n)”方法的功能为:求m 的n 次方
“boolean findFactor(int?m,int?n)”方法的功能为:判断参数的和是否大于 100
定义类实现接口,编写应用程序,调用接口中的 3 个方法,并将调用方法的结果输出。
package com.by.homework3.figure;
public interface Figure {
????public abstract void qiuzhouchang();
????public abstract void qiumianji();
}
package com.by.homework3.figure;
public class Circle implements Figure{
????int banjin;
????public Circle(int banjin) {
????????this.banjin = banjin;
????}
????@Override
????public void qiuzhouchang() {
????????Double zhouchang=2*3.14*banjin;
????????System.out.println("圆的周长:"+zhouchang);
????}
????@Override
????public void qiumianji() {
????????Double mianji=3.14*banjin*banjin;
????????System.out.println("圆的面积:"+mianji);
????}
}
package com.by.homework3.figure;
public class Square implements Figure{
????int bianchang;
????public Square(){
????}
????public Square(int bianchang) {
????????this.bianchang = bianchang;
????}
????public void qiuzhouchang(){
????????int zhouchang=bianchang*4;
????????System.out.println("正方形的周长:"+zhouchang);
????}
????public void qiumianji(){
????????int mianji=bianchang*bianchang;
????????System.out.println("正方形的面积:"+mianji);
????}
}
MediaPlayer有三个实现类,分别是:DVDPlayer,表示DVD播放器;CDPlayer,表示CD播放器; TapePlayer,表示收音机(播放磁带)。
创建一个遥控器Controller类。该遥控器有三个频道,可以分别控制三个设备。部分代码如下:
要求:
①完成MediaPlayer接口及其实现类代码。
package com.by.homework6.mediaPlayer;
interface MediaPlayer{
????void play();
????void open();
????void stop();
}
class DVDPlayer implements MediaPlayer{
????@Override
????public void play() {
????}
????@Override
????public void open() {
????}
????@Override
????public void stop() {
????}
}
class CdPLayer implements MediaPlayer{
????@Override
????public void play() {
????}
????@Override
????public void open() {
????}
????@Override
????public void stop() {
????}
}
class TapePlayer implements MediaPlayer{
????@Override
????public void play() {
????}
????@Override
????public void open() {
????}
????@Override
????public void stop() {
????}
}
class Controller{
????private MediaPlayer[] players;
????public Controller(){
????}
????public void play(int i){
????????players[i].play();
????}
}
public class MediaPlayerTest {
????public static void main(String[]args){
????}
}
②把Controller补充完整,完善其构造方法,并为其增加stop(int i)和open(int i)方法
定义一个Program类,表示一个节目。每一个节目需要多个演员配合,因此每一个Program类中都包含一个属性:Performer数组,表示表演这个节目所需要的演员。
给出Program的部分代码:
class Program{
private Performer[] ps;
public ?Program(){
ps=new Performer[3];
ps[0]=new Singer();
ps[1]=new Dancer();
ps[2]=new Player();
}
}
在现有代码的基础上,为Program增加一个show方法,在这个方法中,调用所有的表演这个节目的所有Performer的perform方法。
?Performer 接口:
public interface Performer {
????/**表演*/
????public abstract void perform();
}
Singer类:
public class Singer implements Performer {
????public void perform() {
????????System.out.println("歌手在表演");
????}
}
????Dancer 类:
public class Dancer implements Performer {
????public void perform() {
????????System.out.println("舞蹈演员在表演");
????}
}
Player类:
public class Player implements Performer {
????public void perform() {
????????System.out.println("演奏者在表演");
????}
}
????Program 类:
public class Program {
????private Performer[] ps;
????public Program(){
????????ps = new Performer[3];
????????ps[0] = new Singer();
????????ps[1] = new Dancer();
????????ps[2] = new Player();
????}
????public void show(){
????????for (int i = 0; i < ps.length; i++) {
????????????ps[i].perform();
????????}
????}
}
????PerformerTest 测试类:
public class PerformerTest {
????public static void main(String[] args) {
????????Program program = new Program();
????????program.show();
????}
}
定义比较排序规则:
根据成绩从大到小排
如果成绩相同,根据头发数量从小到大排
如果头发数量也相同,根据年龄从小到大排
定义测试类,创建学生数组,合理创建若干学生对象,验证排序比较规则
提示:在compare方法中结合嵌套if分支
提交要求:把代码及运行截图放入word文档提交
?Student 类:
public class Student {
????private String name;
????private int age;
????private double score;
????private int hairNum;
????public Student() {
????}
????public Student(String name, int age, double score,int hairNum) {
????????this.name = name;
????????this.age = age;
????????this.score = score;
????????this.hairNum = hairNum;
????}
????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 double getScore() {
????????return score;
????}
????public void setScore(double score) {
????????this.score = score;
????}
????public int getHairNum() {
????????return hairNum;
????}
????public void setHairNum(int hairNum) {
????????this.hairNum = hairNum;
????}
}
????StudentCompare 类:
public class StudentCompare implements Comparator<Student> {
????public int compare(Student o1, Student o2) {
????????if(o1.getScore() < o2.getScore()){
????????????return 1;
????????}else if(o1.getScore() > o2.getScore()){
????????????return -1;
????????}else {
????????????//如果成绩相等,头发少的在前
????????????if(o1.getHairNum() > o2.getHairNum()){
????????????????return 1;
????????????}else if(o1.getHairNum() < o2.getHairNum()){
????????????????return -1;
????????????}else{
????????????????if(o1.getAge() < o2.getScore()){
????????????????????return 1;
????????????????}else if(o1.getAge() > o2.getScore()){
????????????????????return -1;
????????????????}
????????????}
????????????return 0;
????????}
????}
}
????CompareTest 类:
public class CompareTest {
????public static void main(String[] args) {
????????Student[] students = {new Student("小白",23,100,123456),
????????????????new Student("小红",22,88,111111),
????????????????new Student("大红",23,88,111111),
????????????????new Student("小明",20,100,130000),
????????????????new Student("小美",21,90,130000)};
????????Arrays.sort(students, new StudentCompare());
????????//输出排序结果
????????for (int i = 0; i < students.length; i++) {
????????????System.out.println(students[i].getName()+" ?"+students[i].getAge() +" ?"
????????????????????+students[i].getScore() + " " + students[i].getHairNum());
????????}
????}
}