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

Android java基础_类的封装

一.面向对象编程的引入

写一个简单的程序输出张三,李四的名字

class Person {
	String name;
	String getName() {
		return "guangdong "+name;
	}
};


public class Oop {
	public static void main(String args[]) {
		Person p1 = new Person();
		p1.name = "zhangsan";

		Person p2 = new Person();
		p2.name = "lisi";

		System.out.println(p1.getName());
		System.out.println(p2.getName());
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Oop.java
root@ubuntu:/home/topeet/guyilian# java Oop
guangdong zhangsan
guangdong lisi

在以上代码的基础上我们,添加构造函数,在定义对象的时候就把名字传递过去,构造函数的名字跟类名一样,在构造这个对象的时候就会自动执行这个构造方法。

class Person {
	String name;
	String getName() {
		return "guangdong "+name;
	}

	/* construct function */	
	public Person (String n) {
		name = n;
	}
	
};


public class Oop2 {
	public static void main(String args[]) {
		Person p1 = new Person("zhangsan");
		Person p2 = new Person("lisi");

		System.out.println(p1.getName());
		System.out.println(p2.getName());
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Oop2.java 
root@ubuntu:/home/topeet/guyilian# java Oop2
guangdong zhangsan
guangdong lisi

在5代码的基础上,进行构造函数的重载,同时引入this,如果在一个类里面,类的属性与函数的参数名同名,我们加上this修饰表示是当前对象的属性


class Person {
	String name;
	int age;
	String getName() {
		return "guangdong "+name;
	}

	/* construct method */	
	public Person () {
		name = "null";
		age  = 0;
	}

	public Person (String name) {
		this.name = name;
	}

	public Person (String name, int age) {
		this.name = name;
		this.age  = age; 
	}
	
};


public class Oop3 {
	public static void main(String args[]) {
		Person p1 = new Person("zhangsan");
		Person p2 = new Person("lisi");
		Person p3 = new Person();
		Person p4 = new Person("wangwu", 6);

		System.out.println(p1.getName());
		System.out.println(p2.getName());
		System.out.println(p3.getName());
		System.out.println(p4.getName());
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Oop3.java 
root@ubuntu:/home/topeet/guyilian# java Oop3
guangdong zhangsan
guangdong lisi
guangdong null
guangdong wangwu

在以上代码的基础上,我们添加类方法printPerson,那样我们不需要定义具体的类对象我们就可以直接用这个方法,同时我们添加一个类属性count,用来统计人数的多少。


class Person {

	static int count;
	
	String name;
	int age;
	String getName() {
		return "guangdong "+name;
	}

	/* construct method */	
	public Person () {
		count++;
		name = "null";
		age  = 0;
	}

	public Person (String name) {
		count++;
		this.name = name;
	}

	public Person (String name, int age) {
		count++;
		this.name = name;
		this.age  = age; 
	}

	static void printPerson () {
		System.out.println("This is a class of Person");
	}
	
};


public class Oop4 {
	public static void main(String args[]) {
		Person p1 = new Person("zhangsan");
		Person p2 = new Person("lisi");
		Person p3 = new Person();
		Person p4 = new Person("wangwu", 6);

		System.out.println(p1.getName());
		System.out.println(p2.getName());
		System.out.println(p3.getName());
		System.out.println(p4.getName());

		Person.printPerson();
		System.out.println(Person.count);
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Oop4.java 
root@ubuntu:/home/topeet/guyilian# java Oop4
guangdong zhangsan
guangdong lisi
guangdong null
guangdong wangwu
This is a class of Person
4

在以上代码的基础上,我们引入构造代码块, 每实例化一个对象前,都执行;先于构造方法执行。以上例子中我们要在每个构造函数进行count ++,有了构造代码块我们只需要在构造代码块写一次就够了。同时引入静态构造代码块,实例化第一个对象前,执行;只执行一次。


class Person {

	static int count;
	
	String name;
	int age;
	String getName() {
		return "guangdong "+name;
	}

	static {
		System.out.println("static block");		
	}

	{
		System.out.println("construct block");
		count ++;
	}

	/* construct method */	
	public Person () {
		System.out.println("construct method: Person 1");
		name = "null";
		age  = 0;
	}

	public Person (String name) {
		System.out.println("construct method: Person 2");
		this.name = name;
	}

	public Person (String name, int age) {
		System.out.println("construct method: Person 3");
		this.name = name;
		this.age  = age; 
	}

	static void printPerson () {
		System.out.println("This is a class of Person");
	}
	
};


public class Oop5 {
	public static void main(String args[]) {
		Person p1 = new Person("zhangsan");
		Person p2 = new Person("lisi");
		Person p3 = new Person();
		Person p4 = new Person("wangwu", 6);

		System.out.println(p1.getName());
		System.out.println(p2.getName());
		System.out.println(p3.getName());
		System.out.println(p4.getName());

		Person.printPerson();
		System.out.println(Person.count);
	}
}

运行结果:

root@ubuntu:/home/topeet/guyilian# javac Oop5.java 
root@ubuntu:/home/topeet/guyilian# java Oop5
static block
construct block
construct method: Person 2
construct block
construct method: Person 2
construct block
construct method: Person 1
construct block
construct method: Person 3
guangdong zhangsan
guangdong lisi
guangdong null
guangdong wangwu
This is a class of Person
4


http://www.kler.cn/news/233459.html

相关文章:

  • React环境配置
  • 程序的内存模型
  • SpringMVC第一天
  • 动态内存管理(下)
  • SpringMVC第二天
  • 2024年2月5日-2月11日(全面进行+收集虚幻引擎免费资源)
  • 洛谷 P1048 [NOIP2005 普及组] 采药
  • 用python写个类
  • [BeginCTF]真龙之力
  • linux k8s 源码编译及单集群测试
  • C语言----内存函数
  • 【蓝桥杯选拔赛真题64】python数字塔 第十五届青少年组蓝桥杯python 选拔赛比赛真题解析
  • 基于微信江西南昌某汽修店保养预约小程序系统设计与实现 研究背景和意义、国内外现状
  • 探索设计模式的魅力:代理模式揭秘-软件世界的“幕后黑手”
  • 【flutter】报错 cmdline-tools component is missing
  • 跟着pink老师前端入门教程-day23
  • k8s的Deployment部署策略线上踩坑
  • 论文精读的markdown模板——以及用obsidian阅读网页资料做笔记
  • 4核8g服务器能支持多少人访问?2024新版测评
  • 简单的Udp服务器
  • 人类的控制不同于机器的控制
  • MySQL篇之回表查询
  • SegmentAnything官网demo使用vue+python实现
  • LLaMA 2 和 QianWen-14B
  • 制作耳机壳的UV树脂和塑料材质相比劣势有哪些?
  • Amazon Dynamo学习总结
  • mac电脑快捷指令实现拼图
  • django中实现数据迁移
  • 数据结构(C语言)代码实现(八)——顺序栈实现数值转换行编辑程序括号分配汉诺塔
  • 深度学习(13)--PyTorch搭建神经网络进行气温预测