[零基础学JAVA]Java SE面向对象部分-12.面向对象基础(07)
上季内容回忆:1、内部类
2、目标数组的使用
本季主要知识点:
本季关键:static关键词的使用。
本季讲解了Java中static关键词的使用以及单态设计模式。
static的使用
设计一个类erson类,有name、age、city三个属性
classPerson
{
//为了方便,属性暂时不封装
Stringname;
intage;
//所在城默以为
Stringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return名字:+name+,年纪:+age+,城:+city;
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(张三,30);
Personp2=newPerson(李四,31);
Personp3=newPerson(王五,32);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
先把内存结构图画出来
仔细观察,这三个目标的city属性的值都是,就比如说我如今所生成的人都是人。
那么这样的设计就会存在问题:
1、属性的内容重复存在,一切目标都有此属性
2、假如如今假定变为了,假定现已生成了100000个目标,则此刻假如要修复其所在城属性的内容,则肯定要修复100000遍
解决方法:
·一切的目标都一起指向同一个city属性是最好的,有一个目标将city属性修复了,则别的的也会影响。此刻就需要使用static标识city属性了。
classPerson
{
//为了方便,属性暂时不封装
Stringname;
intage;
//所在城默以为
staticStringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return名字:+name+,年纪:+age+,城:+city;
}
};
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(张三,30);
Personp2=newPerson(李四,31);
Personp3=newPerson(王五,32);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
再来看下内存结构
加入static声明以后一切的static类型的属性都保存在了大局数据区当中,一切的目标都一起具有同一个city属性了。
classPerson
{
//为了方便,属性暂时不封装
Stringname;
intage;
//所在城默以为
staticStringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return名字:+name+,年纪:+age+,城:+city;
}
};
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(张三,30);
Personp2=newPerson(李四,31);
Personp3=newPerson(王五,32);
System.out.println(----------修复city属性之前----------);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
System.out.println(----------修复city属性以后----------);
//通过一个目标修复city属性值
p1.city=;
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
验证下作用
一个目标修复static属性以后,别的的一切目标都可以更改了。
可是以上的程序有一些不当,一个城改动是应该由某个人(某个目标)去做的吗?应该是由目标的集合体去做,目标的集合体即是类,所以通常在访问static属性的时候都选用如下格式:
·类称号.static属性这是最合理的。
classPerson
{
//为了方便,属性暂时不封装
Stringname;
intage;
//所在城默以为
staticStringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return名字:+name+,年纪:+age+,城:+city;
}
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(张三,30);
Personp2=newPerson(李四,31);
Personp3=newPerson(王五,32);
System.out.println(----------修复city属性之前----------);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
System.out.println(----------修复city属性以后----------);
//通过一个目标修复city属性值
//最好是通过类称号去修复static属性,即static属性可以被类称号直接访问
Person.city=;
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
验证一下
部分时候也有人把static类型的属性称为类属性(类变量),由于可以直接由类称号进行访问。既然static可以声明属性,则static也可以声明方法,使用static声明的方法称为类方法,也可以由类称号直接调用。
classPerson
{
//为了方便,属性暂时不封装
privateStringname;
privateintage;
//所在城默以为
privatestaticStringcity=;
//只编写一个可以修复city的方法
publicstaticvoidsetCity(Stringcity)
{
this.city=city;
}
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return名字:+name+,年纪:+age+,城:+city;
}
};
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(张三,30);
Personp2=newPerson(李四,31);
Personp3=newPerson(王五,32);
System.out.println(----------修复city属性之前----------);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
System.out.println(----------修复city属性以后----------);
//通过一个目标修复city属性值
//最好是通过类称号去修复static属性,即static属性可以被类称号直接访问
//Person.city=;
Person.setCity();
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
这样验证下有没问题哈~
(Memories from last season:
1. Inner class
2. Use of target array
Main points of knowledge this season:
This season's key: the use of static keywords.
This season explains the use of the static keyword in Java and the monomorphic design pattern.
use of static
Design a class erson class with three attributes: name, age and city
classPerson
{
//For convenience, properties are temporarily not encapsulated
Stringname;
intage;
//The city is silent
Stringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return name: name ,age: age ,city: city;
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(Zhangsan,30);
Personp2=newPerson(Li Si,31);
Personp3=newPerson(Wang5,32);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
First draw the memory structure
Carefully observe, the values ??of the city attributes of these three targets are all, for example, the people I have generated today are all people.
Then there is a problem with this design:
1. The content of the attribute exists repeatedly, and all targets have this attribute
2. If the current assumption is changed, and if 100,000 objects have been generated, then if you want to repair the content of the city attribute, you must repair it 100,000 times
Solution:
·It is best for all the goals to point to the same city attribute. If one goal fixes the city attribute, the others will also be affected. At this point, you need to use static to identify the city property.
classPerson
{
//For convenience, properties are temporarily not encapsulated
Stringname;
intage;
//The city is silent
staticStringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return name: name ,age: age ,city: city;
}
};
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(Zhangsan,30);
Personp2=newPerson(Li Si,31);
Personp3=newPerson(Wang5,32);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
Let's look at the memory structure
After adding the static declaration, all the static type attributes are stored in the overall data area, and all the targets have the same city attribute together.
classPerson
{
//For convenience, properties are temporarily not encapsulated
Stringname;
intage;
//The city is silent
staticStringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return name: name ,age: age ,city: city;
}
};
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(Zhangsan,30);
Personp2=newPerson(Li Si,31);
Personp3=newPerson(Wang5,32);
System.out.println(----------before fix city property----------);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
System.out.println(----------After repairing the city property----------);
//Fix the city property value with a target
p1.city=;
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
Verify the effect
After a target fixes the static property, all other targets can be changed.
However, there are some inappropriate procedures in the above program. Should a city change be done by a certain person (a target)? It should be done by a collection of targets. The collection of targets is a class, so it is usually used to access static properties. When using the following format:
· Class name. static property This is the most reasonable.
classPerson
{
//For convenience, properties are temporarily not encapsulated
Stringname;
intage;
//The city is silent
staticStringcity=;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return name: name ,age: age ,city: city;
}
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(Zhangsan,30);
Personp2=newPerson(Li Si,31);
Personp3=newPerson(Wang5,32);
System.out.println(----------before fix city property----------);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
System.out.println(----------After repairing the city property----------);
//Fix the city property value with a target
//It is best to fix the static property by the class name, that is, the static property can be directly accessed by the class name
Person.city=;
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
verify
In some cases, some people call static type attributes as class attributes (class variables), because they can be accessed directly by the class name. Since static can declare properties, static can also declare methods. Methods declared using static are called class methods, and can also be called directly by the class name.
classPerson
{
//For convenience, properties are temporarily not encapsulated
privateStringname;
privateintage;
//The city is silent
privatestaticStringcity=;
// write only one method that can fix city
publicstaticvoidsetCity(Stringcity)
{
this.city=city;
}
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetInfo()
{
return name: name ,age: age ,city: city;
}
};
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
Personp1=newPerson(Zhangsan,30);
Personp2=newPerson(Li Si,31);
Personp3=newPerson(Wang5,32);
System.out.println(----------before fix city property----------);
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
System.out.println(----------After repairing the city property----------);
//Fix the city property value with a target
//It is best to fix the static property by the class name, that is, the static property can be directly accessed by the class name
//Person.city=;
Person.setCity();
System.out.println(p1.getInfo());
System.out.println(p2.getInfo());
System.out.println(p3.getInfo());
}
};
Check if there is any problem with this~)
页:
[1]