构造方法,this关键字,static关键字,封装
1.构造方法
定义:构造方法是指实例化对象的方法
语法:[修饰符] 类名(参数){ }
根据有无参数分为有参构造和无参构造
1)有参构造
语法:[修饰符] 类名(type 实例变量,int age,String name......){ }
2)无参构造
语法:[修饰符] 类名(){ }
无参构造没有定义时,jvm会自动分配一个无参构造。
注:在使用有参构造时,jvm不会再默认分配一个无参构造,这时我们要自己定义一个无参构造。(反射只能识别无参构造)
public Dog(){
health = 100;
love = 0;
}
public Dog(String _name,int _health,int _love,String _strain){
name = _name;
health = _health;
love = _love;
strain = _strain;
}
3)局部变量和成员变量同名时的优先级
局部变量大于成员变量,所以改变局部变量的变量名称改为_name 或者 将成员变量改为 this.name(推荐后一种方法)
4)有参构造和无参构造是方法重载
2.this关键字
方法调用内存图:

方法调用内存图

this表示对象本身,有一个引用功能,指引用本身对象
this可以调用
1)this 实例变量
this . 变量名 = 成员变量 (可以解决局部变量和成员变量同名)
public Dog2(String name,int health,int love,String strain){
System.out.println("this:"+this);
this.name = name;
this.health = health;
this.love = love;
this.strain = strain;
}
2)this 实例方法
this . 方法名
public Dog(String name,int health,int love,String strain){
this.setName(name);
this.setHealth(health);
this.setLove(love);
this.setStrain(strain);
// showInfo();
this.showInfo();
}
3)this 构造方法
this(有参构造的参数) 或 this()
this(arg1,arg2,…)
public Dog(){
}
public Dog(String name,int health,int love){
this.setName(name);
this.setHealth(health);
this.setLove(love);
}
public Dog(String name,int health,int love,String strain){
//this.setName(name);
//this.setHealth(health);
//this.setLove(love);
// this调用本类的其他构造方法
// System.out.println("test");
this(name,health,love);
this.setStrain(strain);
// showInfo();
//this.showInfo();
}
注调用其他构造方法时,必须将其写在第一句,如
public Dog(String name,int health,int love,String strain){
// this调用本类的其他构造方法
this(name,health,love);
this.setStrain(strain);
3.静态关键字(static)
需求:统计汽车工厂生成了多少量车?
- 统计工厂生成了多少量汽车的功能应该放到类功能上,不应该属于某个对象。
-声明一个变量用于统计个数,这个变量应该被类的实例共享。
-被类的实例共享的区域在方法区(Car.class)
-用static关键字声明这样的变量
static关键字表示静态,可以修改变量,也可以修饰方法。
1)静态变量(类变量)
语法:static 变量名 [ =初始化] static int count = 0;
被static修饰的变量成为静态变量,归类所有,也称为类变量(count),存储与方法区中的静态区,可以被类的实例共享访问。

访问方式
-类名.静态变量(推荐) Car.count;
-对象名.静态变量
2)静态方法(类方法)
语法:public static 返回值类型 方法名(){
return 返回值 ;
}
public static int getCarCount(){
return Car.count;
}
访问方式
-类名.静态方法名 (推荐)
-对象。静态方法名
3)访问权限(类中包含静态成员[ 静态变量和静态方法 ] 和实例成员[ 实例变量和实例方法 ])
静态方法不能访问非静态类成员;实例方法可以访问静态成员(为何如此?)
类加载机制
Car car = new Car(…);
当实例化一个对象时,jvm首先把Car.class加载到方法区
[1]读取Car.class 根据声明的成员变量计算申请内存需要的字节数
[2]读取Car.class 中的静态成员,给静态变量分配空间并初始化。
new Car 申请内存得到一个car对象,此时才有对象的空间。showInfo才可以通过car对象调用。
4.封装
定义:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问。
步骤
-1.将变量私有化
-2.提供公共的设置器和访问器
-3.在设置器和访问器中添加业务校验逻辑
public class Dog{
// 【1】private 私有的,对外不可见
private String name;
private int health;
private int love;
private String strain;
// 【2】提供公共的设置器(setter)和访问器(getter)
public void setName(String name){
// 【3】逻辑校验
if(name.equals("")){
System.out.println("姓名不能为空.");
}else{
this.name = name;
}
}
public String getName(){
return this.name;
}
public void setHealth(int health){
if(health < 0){
System.out.println("健康值不合法.");
this.health = 0;
}else{
this.health = health;
}
}
public int getHealth(){
return this.health;
}
public void setLove(int love){
if(love < 0){
System.out.println("亲密度不合法.");
this.love = 0;
}else{
this.love = love;
}
}
public int getLove(){
return this.love;
}
public void setStrain(String strain){
if(strain.equals("")){
System.out.println("品种不能为空.");
}else{
this.strain = strain;
}
}
public String getStrain(){
return this.strain;
}
public Dog(){
}
public Dog(String name,int health,int love,String strain){
this.setName(name);
this.setHealth(health);
this.setLove(love);
this.setStrain(strain);
}
public void showInfo(){
System.out.print("我的名字叫"+this.name);
System.out.print(",健康值"+this.health);
System.out.print(",亲密度"+this.love);
System.out.println(",我是一只"+this.strain);
}
}
5.静态常量
定义:在程序运行中,如果有一个变量不会发生改变,可以声明一个静态常量,用static final 修饰。
public class Penguin{
private String name;
private int health;
private int love;
private String gender;
// gender为Q仔 将Q仔改为雄
static final String SEX_MALE = "雄";
static final String SEX_FEMALE = "雌";
public class Test02{
public static void main(String[] args){
Penguin penguin = new Penguin("大脚",100,0,Penguin.SEX_MALE);
//用 Penguin.SEX_MALE 而不用Q仔,好处是更新程序时便于更改
}
}
6.代码块
1)普通代码块
普通代码块一般存在于方法或者类、方法等的定义中,普通代码块形成一个作用域。
public class Test03{
public static void main(String[] args){
int count_1 = 10;
// 普通代码块
{
int count_2 = 20;
//System.out.println("count_1:"+count_1);
//System.out.println("count_2:"+count_2);
}
// error
System.out.println("count_2:"+count_2);
}
}
2)构造代码块
构造代码块位于类中。构造代码块在构造方法前执行。构造一个对象执行一次。
public class Person{
String name;
int age;
// 构造代码块
{
System.out.println("构造代码块");
}
public Person(){
System.out.println("构造方法");
}
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
3)静态代码块
静态代码块位于类中,归类所有,用static修饰。在类加载时执行,在构建多个对象时只执行一次
public class Person{
String name;
int age;
static{
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
4)同步代码块(多线程讲解)
总结:静态代码块一般用于初始化静态资源,构造代码块一般用于初始化实例成员。
构造方法,this关键字,static关键字,封装的更多相关文章
- 总结 接口 final关键字 abstract关键字 static 关键字
final关键字: * final 修饰的方法能被继承 不能被重写 * final修饰的类不能被继承 * final 修饰的变量(基本类型)不能被修改 * final 修饰的成员变量必须初始化 局部变 ...
- this关键字、static关键字、block块、封装类以及继承、import、修饰符的初步了解
this关键字 定义 在类的方法定义中使用this关键字代表使用该方法的引用. this即"自己",代表对象本身,谁调用代表谁.在成员方法中或构造器中隐式的传递. this的两种用 ...
- 你真的了解JAVA中对象和类、this、super和static关键字吗
作者:小牛呼噜噜 | https://xiaoniuhululu.com 计算机内功.JAVA底层.面试相关资料等更多精彩文章在公众号「小牛呼噜噜 」 目录 Java对象究竟是什么? 创建对象的过程 ...
- JAVA基础复习与总结<二>构造方法_static关键字_final关键字
构造方法详解 构造器也叫做构造方法(constructor),用于对象的初始化. class Person2 { String name; int age; public Person2(String ...
- this关键字和static关键字
this关键字 普通方法中,this总是指向调用该方法的对象. 构造方法中,this总是指向正要初始化的对象. this区分成员变量和全局变量的作用,在当前类中可以省略. this的常用方法: 让类中 ...
- abstract关键字及static关键字
抽象关键字abstract 抽象类 在类前加上关键字abstract可以将此类变成抽象类.抽象类不允许通过new关键字实例化,但是可一通过其子类向上转型为其创建实例. 抽象类可以有抽象方法,也可以没有 ...
- C语言static关键字
C语言static关键字 static关键字的作用,主要从在程序的生存周期.作用域和在代码段中的位置起作用. 全局变量 静态全局变量 局部变量 静态局部量 生存周期 程序运行到结束 程序运行到结束 函 ...
- 对象的反序列化流_ObjectInputStream和transient关键字瞬态关键字
对象的反序列化流_ObjectInputStream package com.yang.Test.ObjectStreamStudy; import java.io.FileInputStream; ...
- java:构造方法:无参构造/有参构造 this static关键字 静态变量 静态方法 代码块 封装 静态常量。
/*构造方法是一种特殊的方法,专门用于构造/实例化对象,形式:[修饰符] 类名(){ }构造方法根据是否有参数分为无参构造和有参构造*/public class Dog { ...
- 构造方法,this关键字,static关键字,封装,静态变量
1.构造方法 构造方法是一种特殊的方法,是专门用于创建/实例化对象的方法. 构造方法根据是否有参数分为两类:1.无参构造方法 2.有参构造方法 1.1无参构造方法 无参构造方法就是构造方法中没有参数 ...
随机推荐
- c# 对文件的各种操作
C# 获取文件名及扩展名 string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastI ...
- [android] 新闻客户端主界面部分
当我们使用activity加fragment的时候,每个界面都要建立一个fragment,每个fragment里面都要重写onCreate(),onCreateView(),onActivityCre ...
- 【Linux】rpm常用命令及rpm参数介绍
RPM是RedhatPackageManager的缩写,是由RedHat公司开发的软件包安装和管理程序,同Windows平台上的Uninstaller比较类似.使用RPM,用户可以自行安装和管理Lin ...
- Mybatis的自动映射autoMappingBehavior与mapUnderscoreToCamelCase
autoMappingBehavior 在Mybatis的配置文件中添加settings属性的autoMappingBehavior <settings> <setting name ...
- HDU6152
Friend-Graph Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- CodeForces822A
A. I'm bored with life time limit per test 1 second memory limit per test 256 megabytes input standa ...
- JS之ClassName属性使用
一.style与className属性的对比 在前面的style属性学习中,知道了通过style属性可以控制元素的样式,从而实现了行为层通过DOM的style属性去干预变现层显示的目地,但是这种就是不 ...
- requireJS基本概念及使用流程(2)
上一篇我们一起研究了研究requireJS,这一篇我们来说一说requireJS具体的使用过程 其实很简单的,我总结了总结就是分为四步走 第一步:在页面中引入requireJS并且引入入口文件 第二步 ...
- 用venv 配置不同的开发环境
首先使用pip 命令安装 pip install virtualenv (以下是使用win10 操作系统) 1: 在工作目录下创建一个文件夹 mkdir mypython_space 2:然后执行 ...
- 使用CDN做网站的内容加速
1.什么是CDN: CDN的全称是Content Delivery Network,中文的意思就是内容分发网络,简单的讲通过现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的 ...