(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~)