选择题
1. | 【单选题】 下列关于局部内部类的说法错误的是 |
---|
答案:C
填空题
1. | 使用已知的变量,在控制台输出30,20,10,请在空白处填入适当的表达式。 1 //使用已知的变量,在控制台输出30,20,10。 2 class Outer { 3 public int num = 10; 4 class Inner { 5 public int num = 20; 6 public void show() { 7 int num = 30; 8 System.out.println(num ); 9 System.out.println(Inner.this.num ); 10 System.out.println(Outer.this.num ); 11 } 12 } 13 } 14 15 class InnerClassTest { 16 public static void main(String[] args) { 17 Outer.Inner oi = new Outer().new Inner(); 18 oi.show(); 19 } 20 } |
---|
2. | 以下程序的输出结果是: x+y=5 1 public class BwfOuterClass { 2 3 private int x = 1; 4 5 private int y = 2; 6 7 private class BwfInnerClass{ 8 9 private int x = 3; 10 11 public void print(){ 12 13 System.out.println("x+y="+(x+y) ); 14 15 } 16 17 } 18 19 public static void main(String[] args) { 20 21 new BwfOuterClass().new BwfInnerClass().print(); 22 23 } 24 25 } |
---|
编程题
1.抽象方法
【问题描述】定义一个有抽象方法display()的类,以自己姓名拼音命名,如Zhangsan,并编写两个子类SubClassA和SubClassB,两个子类提供不同的display()实现方法,分别输出I am display from A和I am display from B。然后创建一个测试类,以姓名首字母命名,如ZS,在main函数中创建上述两个子类的对象,并调用对应的display方法.
【输入形式】无
【输出形式】
I am display from A
I am display from B
【样例输入】无
【样例输出】
I am display from A
I am display from B
【样例说明】无
【评分标准】全部正确的满分
代码如下:
public class ZS {
public static void main(String[] args) {
new SubClassA().display();
new SubClassB().display();
}
}
abstract class Zhangsan {
public abstract void display();
}
class SubClassA extends Zhangsan {
public void display() {
System.out.println("I am display from A");
}
}
class SubClassB extends Zhangsan {
public void display() {
System.out.println("I am display from B");
}
}
2.动物声音模拟器接口实现
【问题描述】设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:
编写接口Animal,有两个抽象方法:cry()和getAnimalName(),要求实现该接口的各种具体动物类给出自己的叫声和种类名称。
编写模拟器类Simulator,该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型,参数animal可以调用实现Animal接口类重写的cry()方法播放具体动物的声音,调用重写的getAnimalName()方法显示动物种类的名称。
编写实现Animal接口的Dog类和Cat类。
编写主类Application,在主类的main方法中至少包含如下代码:
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
【输入形式】无
【输出形式】
种类名称
种类叫声
【样例输入】无
【样例输出】
Dog
wangwang~~
Cat
miaomiao~~
【样例说明】
【评分标准】
编写的主类Application代码如下:
public class Application {
public static void main(String[] args) {
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
interface Animal {
void cry();
String getAnimalName();
}
class Simulator {
void playSound(Animal animal) {
System.out.println(animal.getAnimalName());
animal.cry();
}
}
class Dog implements Animal {
public void cry() {
System.out.println("wangwang~~");
}
public String getAnimalName() {
return "Dog";
}
}
class Cat implements Animal {
public void cry() {
System.out.println("miaomiao~~");
}
public String getAnimalName() {
return "Cat";
}
}
3.第六章课后编程题1
【问题描述】
定义商品类Goods,包含单价unitPrice和数量account二个属性,方法包括构造方法和价格计算方法totalPrice()。
定义接口VipPrice,包含DISCOUNT属性和reducedPrice()方法,使VIP会员享受商品价格85折待遇。
定义服装子类Clothing,它继承商品类Goods并实现接口VipPrice,并有服装样式style属性、构造方法和toString方法。
编写一个测试类,创建一种服装(200,1,Male),利用toString方法输出服装信息。
【输入形式】无
【输出形式】输出服装的样式、单价和数量
【样例输入】无
【样例输出】style=Male, unitPrice=200.0, account=1
【样例说明】
【评分标准】
编写的测试类代码如下:
public class Test {
public static void main(String[] args) {
Clothing clothing = new Clothing(200, 1, "Male");
System.out.println(clothing);
}
}
class Goods {
private double unitPrice;
private int account;
public void setAccount(int account) {
this.account = account;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public double getUnitPrice() {
return unitPrice;
}
public int getAccount() {
return account;
}
public double totalPrice() {
return unitPrice * account;
}
}
interface VipPrice {
double DISCOUNT = 0.85;
double reducedPrice();
}
class Clothing extends Goods implements VipPrice {
private String style;
public Clothing(double unitPrice, int account, String style) {
setUnitPrice(unitPrice);
setAccount(account);
this.style = style;
}
public double reducedPrice() {
return DISCOUNT * totalPrice();
}
public String toString() {
return "style=" + this.style + ", unitPrice=" + getUnitPrice() + ", account=" + getAccount();
}
}
4.匿名内部类试题
【问题描述】要求使用匿名内部类完成如下要求:
定义一个乐器(Instrument)接口,其中有抽象方法 void play();
在InstrumentTest类中,定义一个方法
void playInstrument( Instrument ins);
并在该类的main方法中调用该方法。
【输入形式】无
【输出形式】I can play this instrument
【样例输入】无
【样例输出】I can play this instrument
【样例说明】输出I can play this instrument
【评分标准】正确得满分。
编写的InstrumentTest类代码如下:
public class InstrumentTest {
public static void main(String[] args) {
Instrument instrument = new Instrument(){
public void play() {
System.out.println("I can play this instrument");
}
};
playInstrument(instrument);
}
public static void playInstrument( Instrument ins){
ins.play();
}
}
interface Instrument{
void play();
}
博主java萌新,有问题可评论区共同交流学习,欢迎大家交流awa