首页 技术 正文
技术 2022年11月19日
0 收藏 751 点赞 5,116 浏览 4474 个字

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关键字,static关键字,封装

方法调用内存图

构造方法,this关键字,static关键字,封装

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),存储与方法区中的静态区,可以被类的实例共享访问。

构造方法,this关键字,static关键字,封装

访问方式

-类名.静态变量(推荐)  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)同步代码块(多线程讲解)

总结:静态代码块一般用于初始化静态资源,构造代码块一般用于初始化实例成员。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905