- 父类的final方法子类不能override,会报错的。
- 静态方法不能被重写,但是能被重新定义
输出为97.3 97.3 50.1 97.3import java.util.*; class Radio { String getFreq() { return "97.3"; } static String getF() { return "97.3"; } } class Ham extends Radio { String getFreq() { return "50.1"; } static String getF() { return "50.1"; } public static void main(String[] args) { List<Radio> radios = new ArrayList<Radio>(); radios.add(new Radio()); radios.add(new Ham()); for (Radio r : radios) System.out.print(r.getFreq() + " " + r.getF() + " "); } }
- 不能重载只返回值不同的方法名。
- 子类默认继承父类不含参数的构造,如果父类写了含参的构造,而没写不含参的,子类就必须显示的调用父类的含参构造,否则报错。(父类若有无参构造,但定义为private的,也报错)
- 变量的可见域
上面代码在return name 处会报错,因为name在父类中为私有变量class Use{ private String name; public Use(){} } class C extends Use{ private String department; public C(){} public String getValue(){ return name; } public static void main(String args[]){ Use p=new Use(); } }
上面代码编译通过 ref.x+x在A类本身中计算,x是可见的class X{ public static void main(String args[]){ A ref=new A(5); A ref2=new A(10); System.out.println(ref.add(ref2)); } } class A{ private int x; A(int x){ this.x=x; } int add(A ref){ return ref.x+x; } }
超出范围for (int i=0;i<= 10;i++){ if( i>6) break; } System.out.println(i);}
//java中变量的作用域 public class scope { public static void main(String args[]) { int x=99; System.out.println("X="+x); { int y=100; System.out.println("x="+x); System.out.println("y="+y); } // System.out.println("Y="+y);//超出范围 } }
public class Scoop {
static int thrower()throws Exception{
return 42;
}
public static void main(String args[]){
try{
int x=thrower();
}
catch(Exception e){
x++;
}
finally{
System.out.println("x="+++x);
}
}
}
上面的try和catch中,try里面定义的int x,在catch和finnaly里面不可见,所以无法编译
public class MouseWash {
static int x = 1;
public static void main(String[] args) {
int x = 2;
for (int i = 0; i < 3; i++) {
if (i == 1)
System.out.print(x + " ");
}
go();
System.out.print(x + " " + i);
}
static void go() {
int x = 3;
}
}
System.out.print里面i超出作用域,编译不通过
若去掉i
public class MouseWash {
static int x = 1;
public static void main(String[] args) {
int x = 2;
for (int i = 0; i < 3; i++) {
if (i == 1)
System.out.print(x + " ");
}
go();
System.out.print(x + " " );
}
static void go() {
int x = 3;
}
}
作用域,遮蔽规则,输出为2 2
5.局部变量不能有访问修饰符
public class Jail {
private int x = 4;
public static void main(String[] args) {
protected int x = 6; //不能有protected
new Jail().new Cell().slam();
}
class Cell {
void slam() { System.out.println("throw away key " + x); }
}
}如果修改为“int x = 6”,这段代码可以编译,输出的结果是“throw away key 4”。第6 行创
建了一个匿名对象Jail,一个匿名的Cell 对象,然后调用了这个匿名对象的slam()方法。
内部类访问了其封闭类的私有变量。
6.java强制类型转换
class Dog {
}
class Beagle extends Dog {
}
public class Kennel {
public static void main(String args[]) {
Beagle b1 = new Beagle();
Dog d1 = new Dog();
Dog d2 = b1;
//Beagle b2 = (Beagle) d1;//d1是dog
Beagle b3 = (Beagle) d2;//d2指向的是beagle
// Beagle b4 = d2;
}
}
7.。StringBuffer 类没有重写equals()方法,所以两个有相同值的不同StringBuffer
对象根据equals()方法来判断是不相等的。另一方面,String 类的equals()方法被重写了,两
个有相同值的不同String 对象根据equals()方法来判断是相等的。
对象根据equals()方法来判断是不相等的。另一方面,String 类的equals()方法被重写了,两
个有相同值的不同String 对象根据equals()方法来判断是相等的。
8.多态不能应用于泛型的类型参数。
2. import java.util.*;
3. class Cereal { }
4. public class Flakes extends Cereal {
5. public static void main(String[] args) {
6. List<Flakes> c0 = new List<Flakes>();//不能,list是抽象类
7. List<Cereal> c1 = new ArrayList<Cereal>();
8. List<Cereal> c2 = new ArrayList<Flakes>();//F
9. List<Flakes> c3 = new ArrayList<Cereal>();//F
10. List<Object> c4 = new ArrayList<Flakes>();//F
3. class Cereal { }
4. public class Flakes extends Cereal {
5. public static void main(String[] args) {
6. List<Flakes> c0 = new List<Flakes>();//不能,list是抽象类
7. List<Cereal> c1 = new ArrayList<Cereal>();
8. List<Cereal> c2 = new ArrayList<Flakes>();//F
9. List<Flakes> c3 = new ArrayList<Cereal>();//F
10. List<Object> c4 = new ArrayList<Flakes>();//F
11. ArrayList<Cereal> c5 = new ArrayList<Flakes>();//F
12. }
13. }
12. }
13. }
9.关于override:试图重写父类的final方法,编译器会报错。写private方法不会报错,但是没有实现重写,只是一个恰好同名的不同方法。
下面代码,输出为private f()
public class PrivateOverride {
private void f() {
System.out.println("private f()");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PrivateOverride po = new Derived();
po.f();
}
}
class Derived extends PrivateOverride {
public void f() {
System.out.println("public f()");
}
}
10.一个类如果包含抽象方法,则必须声明为抽象类,如果子类中没有实现父类中所有的抽象方法,子类也必须被声明为抽象类;只有子类实现了父类中所有的抽象方法,才可以不声明为抽象类抽象类实现接口时,可以不实现接口的所有方法
interface Horse {
public void nicker();
}
abstract class Eyra implements Horse {
public void nicker(int loud) { }
}
可以通过编译,nicker并不是对接口中的实现,而是一个新的方法
11.sort和reverse是Collections类里面的,而不是Collection
Collections中方法只能对List使用,不能对Set使用,编译不能通过!
Set里面放的是不相同的东西(但是要注意那货有木有重写equals!)
import java.util.*;
public class MyFriends {
String name;
MyFriends(String s) { name = s; }
public static void main(String[] args) {
Set<MyFriends> ms = new HashSet<MyFriends>();
ms.add(new MyFriends("Bob"));
System.out.print(ms + " ");
ms.add(new MyFriends("Bob"));
System.out.print(ms + " ");
ms.add(new MyFriends("Eden"));
System.out.print(ms + " ");
}
public String toString() { return name; }
}
[Bob] [Bob, Bob] [Eden, Bob, Bob]。MyFriends 类没有重写equals()方法,所以在添加到HashSet 中时,两个Bob 对象会被认为是不相等的。
12.当一个类同时声明implements 和extends 时,extends 声明放在前边
13.循环的题目要注意会不会死循环
String s = "123 888888 x 345 -45";
Scanner sc = new Scanner(s);
while(sc.hasNext())
if(sc.hasNextShort())
System.out.print(sc.nextShort() + " ");
上为死循环14.布尔表达式中,不能用int,java里面int和boolean不通用,会编译器报错!
15.static public void main 也可以
16.若不指定this,变量重名时,都用形参中的