Android java基础_类的继承

一.Android Java基础_类的继承

先封装一个persion类,在persion的基础上定义Student类,并基础persion类。

子类能访问父类的成员函数。

class Person {
	private int age;

	public void setAge(int age) {
		if (age < 0 || age > 200)
			age = 0;
		else {
			this.age = age;
		}
	}

	public int getAge() {
		return age;
	}	
}

class Student extends Person{
}

public class Ext {
	public static void main (String args[]) {
		Student stu = new Student();

		stu.setAge(10);

		System.out.println(stu.getAge());
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Ext.java 
root@ubuntu:/home/topeet/guyilian# java Ext
10

在上面的代码基础上,我们的子类继承了父类,我们可以添加自己的属性还有方法.

class Person {
	private int age;

	public void setAge(int age) {
		if (age < 0 || age > 200)
			age = 0;
		else {
			this.age = age;
		}
	}

	public int getAge() {
		return age;
	}	
}

class Student extends Person{
	private String school;

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

	public String getSchool() {
		return school;
	}	

	public Student(String school) {
		this.school = school;
	}
	
}

public class Ext2 {
	public static void main (String args[]) {
		Student stu = new Student("ustc");

		stu.setAge(10);

		System.out.println(stu.getAge());
		System.out.println(stu.getSchool());
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Ext2.java 
root@ubuntu:/home/topeet/guyilian# java Ext2
10
ustc

子类继承父类的方法和属性都可以进行覆写,我们在子类覆写父类的printInfo方法。

class Person {
	private int age;

	public void setAge(int age) {
		if (age < 0 || age > 200)
			age = 0;
		else {
			this.age = age;
		}
	}

	public int getAge() {
		return age;
	}	

	public void printInfo() {
		System.out.println("age = "+age);
	}
}

class Student extends Person{
	private String school;

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

	public String getSchool() {
		return school;
	}	

	public Student(String school) {
		this.school = school;
	}

	/* override */
	public void printInfo() {
		System.out.println("school = "+school+"; age = "+getAge());
	}
	
}

public class Ext3 {
	public static void main (String args[]) {
		Student stu = new Student("ustc");

		stu.setAge(10);

		System.out.println(stu.getAge());
		System.out.println(stu.getSchool());
		stu.printInfo();
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Ext3.java 
root@ubuntu:/home/topeet/guyilian# java Ext3
10
ustc
school = ustc; age = 10

实例化子类对象时,先调用父类的构造方法,再调用子类的构造方法,super()函数在子类中可以指定调用父类构造函数的类型。

class Person {
	private int age;

	public void setAge(int age) {
		if (age < 0 || age > 200)
			age = 0;
		else {
			this.age = age;
		}
	}

	public int getAge() {
		return age;
	}	

	public void printInfo() {
		System.out.println("age = "+age);
	}

	public Person () {System.out.println("Person ()");}
	public Person (int age) {
		System.out.println("Person (int age)");
		this.age = age;
	}
	
}

class Student extends Person{
	private String school;

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

	public String getSchool() {
		return school;
	}	

	public Student(String school) {
		/* will call the super() */
		//super();
		super(5);
		System.out.println("Student(String school)");
		this.school = school;
	}

	/* override */
	public void printInfo() {
		System.out.println("school = "+school+"; age = "+getAge());
	}
	
}

public class Ext4 {
	public static void main (String args[]) {
		Student stu = new Student("ustc");

		//stu.setAge(10);

		System.out.println(stu.getAge());
		System.out.println(stu.getSchool());
		stu.printInfo();
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Ext4.java 
root@ubuntu:/home/topeet/guyilian# java Ext4
Person (int age)
Student(String school)
5
ustc
school = ustc; age = 5

抽象类规定子类必须实现的方法,起“模板”作用,缺点不能实例化对象,子类必须覆写全部抽象方法。

abstract class Father {
	private int money;	

	public int getMoney() {return money; }
	public void setMoney(int money) {this.money = money; }

	public abstract void study();
}

class Son extends Father{
	public void study() {System.out.println("I am study"); }
	
}

public class Ext6 {
	public static void main (String args[]) {
		//Father f = new Father();
		Son son = new Son();
		son.study();

	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Ext6.java 
root@ubuntu:/home/topeet/guyilian# java Ext6
I am study

作用:跟抽象类相似,起“模板”作用;子类可以继承多个接口,突破“单继承”的限制。

abstract class Father {
	private int money;	

	public int getMoney() {return money; }
	public void setMoney(int money) {this.money = money; }

	public abstract void study();
}

class Son extends Father{
	public void study() {System.out.println("I am study"); }
	
}

public class Ext6 {
	public static void main (String args[]) {
		//Father f = new Father();
		Son son = new Son();
		son.study();

	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Ext7.java
root@ubuntu:/home/topeet/guyilian# java Ext7
InterfaceB
I am study

你可能感兴趣的:(Android,android,java,开发语言)