当前位置: 首页 > article >正文

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


http://www.kler.cn/a/234016.html

相关文章:

  • 【设计模式】行为型模式(二):策略模式、命令模式
  • Python数据类型(一):bool布尔类型
  • 计算机毕业设计Python+Neo4j知识图谱医疗问答系统 大模型 机器学习 深度学习 人工智能 大数据毕业设计 Python爬虫 Python毕业设计
  • 设计模式:工厂方法模式和策略模式
  • 机器学习day3-KNN算法、模型调优与选择
  • JavaScript高级程序设计基础(四)
  • python如何用glob模块匹配路径
  • Lua Global环境
  • 时间序列预测——BiGRU模型
  • 应急响应-挖矿木马-常规处置方法
  • notepad++成功安装后默认显示英文怎么设置中文界面?
  • 突破编程_C++_面试(基础知识(10))
  • 学习笔记——ENM模拟
  • 微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
  • 零基础学编程从入门到精通,系统化的编程视频教程上线,中文编程开发语言工具构件之缩放控制面板构件用法
  • Centos 7系统安装proftpd-1.3.8过程
  • 图像的旋转不变特性及应用
  • React18原理: Fiber架构下的单线程CPU调度策略
  • 代码随想录-背包问题
  • vue3 之 商城项目—详情页
  • 双非本科准备秋招(21.1)—— 力扣二叉搜索树
  • C++ //练习 5.4 说明下列例子的含义,如果存在问题,试着修改它。
  • 随机MM引流源码PHP开源版
  • RabbitMQ-1.介绍与安装
  • django中实现观察者模式
  • Python面试题7-12