编程题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;
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();
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();创建对象
}
}