上题回顾与解析
public class Question15 {
public static void main(String[] args){
System.out.println("hello");
http://hello.com;
System.out.println(" world");
}
}
这是一个有点诡异的问题。程序并没有做什么特殊的事情,而是直接打印hello world
。这里其实使用了java中一个鲜为人知的特性「语句标号」,可在任意语句前防止语句标号,它只作提醒的作用。
一般都不使用标号,很容易会引起不必要的混淆与误解。如果确实需要使用标号,这样格式化更合理:
public class Question15 {
public static void main(String[] args){
System.out.println("hello");
http: //hello.com;这里的//其实是注解的作用
System.out.println(" world");
}
}
每日一题
下面程序,输出什么?
public class Point {
protected int x,y;
private String name;
Point(int x,int y){
this.x = x;
this.y = y;
this.name = makeName();
}
protected String makeName(){
return "["+ x + "," + y +"]";
}
@Override
public String toString(){
return name;
}
}
class ColorPoint extends Point{
private String color;
ColorPoint(int x, int y, String color){
super(x,y);
this.color = color;
}
@Override
protected String makeName(){
return super.makeName() + ":" + color;
}
public static void main(String[] args){
System.out.println(new ColorPoint(4, 2, "blue"));
}
}