您的位置:首页 > 编程语言 > Java开发

java_对象(this和成员变量和Static修饰符)

2015-09-03 16:39 447 查看
/*

需求: 使用java类描述一个动物。

问题:存在同名的成员变量与局部变量时,在方法的内部访问的是局部变量(java 采取的是就近原则的机制访问的。)。

this关键字:

this关键字代表了所属函数的调用者对象。

this关键字作用:

1. 如果存在同名成员变量与局部变量时,在方法内部默认是访问局部变量的数据,可以通过this关键字指定访问成员变量的数据。

2. 在一个构造函数中可以调用另外一个构造函数初始化对象。

this关键字调用其他的构造函数要注意的事项:

1. this关键字调用其他的构造函数时,this关键字必须要位于构造函数中 的第一个语句。

2. this关键字在构造函数中不能出现相互调用 的情况,因为是一个死循环。

this关键字要注意事项:

1. 存在同名的成员变量与局部变量时,在方法的内部访问的是局部变量(java 采取的是“就近原则”的机制访问的。)

2. 如果在一个方法中访问了一个变量,该变量只存在成员变量的情况下,那么java编译器会在该变量的 前面添加this关键字。

*/
class Animal{

String name ;  //成员变量

String color;

public Animal(String n , String c){
name = n;
color = c;
}

//this关键字代表了所属函数的调用者对象
public void eat(){
//System.out.println("this:"+ this);
String name = "老鼠"; //局部变量
System.out.println(name+"在吃..."); //需求: 就要目前的name是成员变量的name.

}
}

class Demo6
{
public static void main(String[] args)
{
Animal dog = new Animal("狗","白色");  //现在在内存中存在两份name数据。

Animal cat = new Animal("猫","黑色");
cat.eat();

}
}


/*

this关键字调用其他的构造函数要注意的事项:

1. this关键字调用其他的构造函数时,this关键字必须要位于构造函数中 的第一个语句。

2. this关键字在构造函数中不能出现相互调用 的情况,因为是一个死循环。

*/

class Student{

int id;  //身份证

String name;  //名字

//目前情况:存在同名 的成员 变量与局部变量,在方法内部默认是使用局部变量的。
public Student(int id,String name){  //一个函数的形式参数也是属于局部变量。
this(name); //调用了本类的一个参数的构造方法
//this(); //调用了本类无参的 构造方法。
this.id = id; // this.id = id 局部变量的id给成员变量的id赋值
System.out.println("两个参数的构造方法被调用了...");
}

public Student(){
System.out.println("无参的构造方法被调用了...");
}

public Student(String name){
this.name = name;
System.out.println("一个参数的构造方法被调用了...");
}

}

class Demo7
{
public static void main(String[] args)
{
Student s = new Student(110,"铁蛋");
System.out.println("编号:"+ s.id +" 名字:" + s.name);
/*

Student s2 = new Student("金胖子");
System.out.println("名字:" + s2.name);
*/
}
}


/*

static(静态)

需求:描述一下传智学生类。 都是中国人….

目前存在的问题: 所有的学生都是中国 的,有n个学生就会有n份中国的 数据存内存中,这样子

会浪费内存。

目前方案: 把“中国”这个数据移动 到数据共享区中,共享这个数据给所有的Student对象使用即可。

问题2: 如何才能把这个数据移动 到数据共享区中共享呢?

解决方案: 只需要使用static修饰该数据即可。

静态的成员变量只会在数据共享区中维护一份,而非静态成员变量的数据会在每个对象中都维护一份的。。

*/

class Student{

String name;

//使用了static修饰country,那么这时候country就是一个共享的数据。

static  String  country  = "中国";    //国籍

//构造函数
public Student(String name){
this.name = name;
}
}

class Demo9 {

public static void main(String[] args)
{
Student s1 = new Student("张三");
Student s2 = new Student("陈七");

s1.country = "小日本";
System.out.println("姓名:"+s1.name+" 国籍:"+ s1.country); //  中国
System.out.println("姓名:"+s2.name+" 国籍:"+ s2.country); // 小日本
}
}


/*

static(静态\修饰符)

1. static修饰成员变量 :如果有数据需要被共享给所有对象使用时,那么就可以使用static修饰。

静态成员变量的访问方式:

方式1: 可以使用对象进行访问。
格式: 对象.变量名。

方式二: 可以使用类名进行访问。
格式: 类名.变量名;

注意:
1. 非静态的成员变量只能使用对象进行访问,不能使用类名进行访问。
2. 千万不要为了方便访问数据而使用static修饰成员变量,只有成员变量的数据是真正需要被共享的时候
才使用static修饰。

static修饰成员变量的应用场景: 如果一个数据需要被所有对象共享使用的时候,这时候即可好实用static修饰。

2. static修饰成员函数:


*/

class Student{

static  String name;  //非静态成员变量

static  String  country  = "中国";      //静态的成员变量

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

class Demo10 {

public static void main(String[] args)
{
Student s1 = new Student("狗娃");
Student s2 = new Student("狗剩");

//System.out.println("国籍:"+ Student.country);
System.out.println("名字:"+ s1.name);
System.out.println("名字:"+ s2.name);
}
}


/*

需求: 统计一个类被使用了多少次创建对象,该类对外显示被创建的次数。

*/

class Emp{

//非静态的成员变量。
static  int count = 0;  //计数器

String name;

//构造代码块
{
count++;
}

public Emp(String name){
this.name = name;

}

public Emp(){  //每创建一个对象的时候都会执行这里 的代码

}

public void showCount(){
System.out.println("创建了"+ count+"个对象");
}
}

class Demo11
{
public static void main(String[] args)
{
Emp e1 = new Emp();
Emp e2 = new Emp();
Emp e3 =new Emp();
e3.showCount();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: