閱讀158 返回首頁    go 技術社區[雲棲]


馬士兵J2SE-第三章-麵向對象-static、繼承、重寫、構造函數

 

static關鍵字 靜態成員變量

  

public class Cat{
	private static int sid=0;
	private String name;
	int id;
	
	Cat(String name){
		this.name=name;
		id=sid++;
	}
	
	public void info(){
		System.out.println("My name is "+name+" No."+id);
	}
	
	public static void main(String[] args) {
		Cat tom=new Cat("tom");
		tom.sid=100;
		Cat jack=new Cat("jack");

		tom.info();
		jack.info();
	}
}


輸出:

My name is tom No.0
My name is jack No.100

 

 

 使用ECLIPSE ALT+SHIFT+S自動快速生成的代碼

類的繼承與權限控製

class Person {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
}

class Student extends Person {
	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	
}

public class test {
	public static void main(String[] args) {
		Student student = new Student();
		student.setName("zzk");
		student.setAge(23);
		student.setSchool("MIT");
		System.out.println(student.getName());
		System.out.println(student.getAge());
		System.out.println(student.getSchool());
	}
}

zzk
23
MIT


重寫:

class Person {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
    
	public String getInfo() {
		return "Name: "+name+"\n"+"age:"+age;
	}
}


class Student extends Person {
	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String getInfo() {
		return "Name: "+getName()+"\nage"+getAge()+"\nschool:"+school;
	}
}


public class test {
	public static void main(String[] args) {
		Student student=new Student();
		Person person=new Person();
		person.setName("Tom");
		person.setAge(28);
		student.setName("Jack");
		student.setAge(23);
		student.setSchool("MIT");
		System.out.println(person.getInfo());
		System.out.println(student.getInfo());
	}
}


輸出:

Name: Tom
age:28
Name: Jack
age23
school:MIT

 

構造函數與一般成員函數在繼承中的區別

class A {
	protected void print(String s) {
		System.out.println(s);
	}
	
	A() {print("A()");};
	
	public void f() {print("A:f()");};
}

class test extends A {
	test() {print("B()");};
	public void f() {print("B:f()");}
    
	public static void main(String[] args) {
		test b=new test();
		b.f();
	}
}


輸出:

A()
B()
B:f()


 

構造方法調用

class Person {
	private String name;
	private String location;
	
	Person(String name) {
		this.name=name;
		location="beijing";
	}
	
	Person(String name,String location) {
		this.name=name;
		this.location=location;
	}
	
	public String info() {
		return "name: "+name+" location: "+location; 
	}
}

class Student extends Person {
	private String school;
	Student(String name,String school) {
        this(name,"beijing ",school);
	}
	Student(String n,String l,String school) {
		super(n,l);
		this.school=school;
	}
	
	public String info() {
		return super.info()+" school: "+school;
	}
}

public class test {
	public static void main(String[] ars) {
		Person p1=new Person("A");
		Person p2=new Person("B","shanghai");
		Student s1=new Student("C","S1");
		Student s2=new Student("C","shanghai","S2");
		System.out.println(p1.info());
		System.out.println(p2.info());
		System.out.println(s1.info());
		System.out.println(s2.info());
		
	}
}

輸出:


name: A location: beijing
name: B location: shanghai
name: C location: beijing  school: S1
name: C location: shanghai school: S2

 

 

 

//構造Teacher類,繼承Person類
//增加“職稱”(String)屬性
//具有和Student類類似的重載構造方法
//重寫Person類的info()方法,增加“職稱”信息

class Person {
	private String name;
	private String location;
	
	Person(String name) {
		this.name=name;
		location="beijing";
	}
	
	Person(String name,String location) {
		this.name=name;
		this.location=location;
	}
	
	public String info() {
		return "name: "+name+" location: "+location; 
	}
}

class Teacher extends Person {
	private String profession;
	Teacher(String name,String profession) {
		this(name,"beijing",profession);
	}
	Teacher(String n,String l,String profession) {
		super(n,l);
		this.profession=profession;
	}
	public String info() {
		return super.info()+" profession: "+profession;
	}
}


class Student extends Person {
	private String school;
	Student(String name,String school) {
        this(name,"beijing ",school);
	}
	Student(String n,String l,String school) {
		super(n,l);
		this.school=school;
	}
	
	public String info() {
		return super.info()+" school: "+school;
	}
}

public class test {
	public static void main(String[] ars) {
		Person p1=new Person("A");
		Person p2=new Person("B","shanghai");
		Student s1=new Student("C","S1");
		Student s2=new Student("C","shanghai","S2");
		Teacher t1=new Teacher("D","phd");
		Teacher t2=new Teacher("D","haerbin","doc");
		System.out.println(p1.info());
		System.out.println(p2.info());
		System.out.println(s1.info());
		System.out.println(s2.info());
		System.out.println(t1.info());
		System.out.println(t2.info());
		
	}
}
輸出:



name: A location: beijing
name: B location: shanghai
name: C location: beijing  school: S1
name: C location: shanghai school: S2
name: D location: beijing profession: phd
name: D location: haerbin profession: doc


 

 

 


 

 

最後更新:2017-04-02 06:52:05

  上一篇:go 批量更新重複數據
  下一篇:go Android係列教程之七:EditText使用詳解-包含很多教程上看不到的功能演示