2018年Java答案

编程题1

class Person{
    private String name;
    private int age;
    public Person(String a,int b){
        name=a;
        age=b;
    }
    public String toString(){
        return name+","+age;
    }
}
class Student extends Person{
    private String dept;
    public Student(String a,int b,String c){
        super(a,b);
        dept=c;
    }
    public String toString(){
        return super.toString()+","+dept;
    }
}
public class Test{
    public static void main(String[] args){
        Person r1=new Person("LiPing",18);
        System.out.println(r1);
        Student r2=new Student("Mary",20,"Computer");
        System.out.println(r2);
    }
}

编程题2

interface I{
    int calSize();
} 
class Image{
    int width,height;
    public static void main(String[] args){
        BinaryImage b=new BinaryImage(1,1);
        GreyImage g=new GreyImage(1,1);
        ColorImage c=new ColorImage(1,1);
        System.out.println(b.calSize());
        System.out.println(g.calSize());
        System.out.println(c.calSize());
    }
}
class BinaryImage extends Image implements I{
    BinaryImage(int a,int b){
        width=a;
        height=b;
    }
    public int calSize(){
        return width*height;
    }
}
class GreyImage extends Image implements I{
    GreyImage(int a,int b){
        width=a;
        height=b;
    }
    public int calSize(){
        return width*height*8;
    }
}
class ColorImage extends Image implements I{
    ColorImage(int a,int b){
        width=a;
        height=b;
    }
    public int calSize(){
        return width*height*24;
    }
}

400

class Test {
public static void main(String[] args) {
int w=0;
outer:
for(int x=0; x < 5; x++){
for(int y=0; y < 100; y++){
if (x==3)
continue outer;  //是跳出一次循环,而不是所有循环  outer是标记
w++;
} }
System.out.println("w="+ w);
}
}

11,0

class SuperClass {
    int x;
    void method0() {
        method1();
    }
    private void method1() {
        x=11;
    }
}
public class SubClass extends SuperClass {
    int x;
    private void method1() {   //注意不是构造器
        x=2;
    }
    public static void main(String[] args) {
        SubClass rSub=new SubClass();
        SuperClass rSuper=rSub;//默认构造器
        rSub.method0();//如果删去,答案就是0,0
        System.out.println(rSuper.x+","+rSub.x);
    }
}

init

class Q{
static{
System.out.println("static init");
}
}
public class Q1{
{
System.out.println("init");  //这是代码块,在公共类的代码块
}
public static void main(String[] args) {
Q rQ;//定义变量
new Q1();创建对象
}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值