马士兵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


 

 

 


 

 

上一篇:《树莓派渗透测试实战》——1.8 克隆树莓派SD卡


下一篇:天气预报抓取、解析(传递抓取的info参数)功能模块实现(代码)