Inheritance Syntax
When you using the keyword extends followed by the name of the base class,when you do this,you automatically get all the fields and methods in the base class.
When you created an object of the derived class,it contains within it a subobject of the base class.This subobject is the same as if you had created an object of the base class by itself,it’s just that from the outside,the subobject of the base class is the wrapped within the derived-class object.
e.g
class Art {
Art() {
System.out.println("Art's constructor");
}
}
class Drawing extends Art {
Drawing() {
System.out.println("Drawing's constructor");
}
}
public class Cartoon extends Drawing{
Cartoon() {
System.out.println("Cartoon's constructor");
}
public static void main(String[] args) {
Cartoon cart=new Cartoon();
}
}
无论写不写,子类总是默认首先执行父类无参的构造
The call to the base-class constructor must be the first thing you do in the derived-class constructor.
The Final keyword,with a primitive,final makes the value a constant.but with a object reference,final makes the reference a constant(and the object itself could be changed)
9/23/2009 2:18:34 PM
今天上课的例题,map加反射,完全做不出来
import java.lang.reflect.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class TestFill {
public static Object getInstance (String name,Map<String,Object> map) throws Exception {
Class c=Class.forName(name);
Object obj=c.newInstance();
Set<String> set=map.keySet();
for(String key:set) {
String mName="set"+key.substring(0, 1).toUpperCase()+key.substring(1);
Field f=c.getDeclaredField(key);
Method m=c.getMethod(mName,f.getType());
m.invoke(obj, map.get(key));
}
return obj;
}
public static void main(String[] args) throws Exception {
Map<String,Object> map=new HashMap<String,Object>();
map.put("name","zhang");
map.put("age", 23);
map.put("id",3);
Object obj=TestFill.getInstance("date0923.Student", map);
System.out.println(obj);
}
}
public class Student {
private int age;
private String name;
private int id;
public Student() {
super();
System.out.println("Stu1");
}
public Student(int age,String name,int id) {
super();
System.out.println("Stu2");
this.setAge(age);
this.setName(name);
this.setId(id);
}
@Override
public String toString() {
return name+","+id+","+age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
三个方法的重写,
@Override
equals,hashCode,toString
String 背下来,String的各项办法
List/Map用法
Integer.parseInt();
Double.parseDouble();
Try/catch 或者throws异常,学会看异常
多看API
Producing the right behavior
Circle reference |
Shape draw() erase()
|
Circle draw() erase()
|
Square draw() erase()
|
Triangle draw() erase()
|
Shape s=new Circle(); Circle is a shape by inheritance.
sub actually contains two fields called field,its own and it gets from Super.v