2013年10月29日星期二

public variable of type any harm .??

Hello everyone , I have a code in the class too much , I feel a bit chaotic , I thought of classification, on the establishment of a number of Class document, but there are some of these multiple Class variable is public, such as mCurrentState such a variable , you need to be accessed in a number of Class .


My approach is to create a public master class types of variables , several other forms of access to the use of such Class :
mMainActivity.mCurrentState = MyState.start;
...........
if (mMainActivity.mCurrentState == MyState.start) {
........
}

Will the next such statement has any questions for me , define the types of variables such as public any harm , thank you ! !



------ Solution ------------------------------------ --------
see requirements , if it is simple attributes , the attribute set are direct assignment , no need to check the validity of such arguments , the direct public good, easy, too lazy to write so much setter, getter.
personally feel no need to write all attributes setter, getter.
But having said that , the actual project Java Bean property will be set to private, then write setter, getter, according to specifications written to, because do not own handwritten code , IDE generated directly , why not , and so people say professional right .

------ For reference only ---------------------------------- -----
any harm , ye that ye used to use , mainly to see how easy it was to use .
------ For reference only -------------------------------------- -
general will attribute is set to private, and then get, set methods to access and set up , so was back before the assignment if the value or do some processing , you can get, set inside the method for processing, this package logic processing .
------ For reference only -------------------------------------- -





------ For reference only ---------------------------------------
OOD is not object-oriented design because your content too much in a class assigned to another class to go . Class is designed to sense , when it needs to classify into multiple categories. Three characteristics of the class encapsulation, inheritance , polymorphism, encapsulation requires property in the best control modifier is private property , so that the properties of each class is equivalent to each other is hidden , other classes will not have permission to directly modify other classes attributes defined only through public methods defined in the class modify the properties. Said a lot , hope lz look out for themselves .
------ For reference only ---------------------------- -----------
public variables other program's code is easy to modify . Generally defined as a private variable or better , and only use it for other classes set to public
------ For reference only ------------------- --------------------

If the program from being modified difficulty speaking , obj.attr = 10 and obj.setAttr ( 10 ) What is the difference ? No difference.
So this argument is wrong.
------ For reference only -------------------------------------- -

If the program from being modified difficulty speaking , obj.attr = 10 and obj.setAttr (10) What is the difference ? No difference.   
So this argument is wrong.  

but if you do not want to modify , then other classes , private variables set methods can not write it . The public variable of type can still modify it .
------ For reference only -------------------------------------- -
establish a parent class , all the spin-off classes inherit the parent class , all subclasses need to use the object definitions to the parent class to go inside, and these objects is defined as the public access modifier or protected, if only it is recommended to use a subclass defined as protected. this is a workaround.
In addition , with regard to the definition of access modifiers and permissions , you need to brush up my foundation .
------ For reference only -------------------------------------- -
fact, take a look at the java source code you will probably understand its importance , such as arraylist has size of this property , get it is, size (), you add the size relationship to the index, there are many important ways , if set to public people just do not mess it changed , so use private, and a getter of the method, so you can look but not touch .
that is private is actually a constraint on the other program .
------ For reference only -------------------------------------- -
public inside the class you write can be directly changed by instantiating objects . Such insecurity
------ For reference only ----------------------------------- ----
get set is not mandatory , the setting for the public on the public, do not need to have used the so-called get set method .
------ For reference only -------------------------------------- -
existence of security risks , so try to attribute private
------ For reference only -------------------------- -------------
public itself is no problem , just look at the code logic

------ For reference only ---------------------------------- -----
public only proper use , do not flood , or as you put the key in the door of your house up . Who can open the door
------ For reference only --------------------------------- ------
Oh, thank you for your reply , probably understand some of it. Results posted it .
------ For reference only ------- --------------------------------
several solutions I think the landlord can reference
construct a class variable on the inside to make the generic attribute classification subclasses to do it is to construct an inner class within the class trouble spots
better approach is to put all global variables abstracted into a new class of general variables encapsulate get set method can also be invoked if the variable will be the only class to do into a single-case model
usually constructed class must abide by the high cohesion and low coupling concept will increase the public with more degree of coupling is not recommended

没有评论:

发表评论