getter and setter In java
2014-09-24 21:32
274 查看
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}In MyEclipse, you can just generate setter and getter by:
choose the parameter defined -> source -> generate getter and setter
The getter and setter methods can help hide internal state and requiring all interaction to be performed
through an object's methods is known as data
encapsulation — a fundamental principle of object-oriented programming.
In fact:
Your object uses setter to:
restrict and validate data passed to the setter
hide its inner data structure (other object are interested by a service not how the service is built, this can also include optimisation)
preserve its integrity in every state (changing other fields if required)
it will use getter to
format data in output as desired by the client
control a sequence of services (for instance it will provide data if and only if a connection has been established)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}In MyEclipse, you can just generate setter and getter by:
choose the parameter defined -> source -> generate getter and setter
The getter and setter methods can help hide internal state and requiring all interaction to be performed
through an object's methods is known as data
encapsulation — a fundamental principle of object-oriented programming.
In fact:
Your object uses setter to:
restrict and validate data passed to the setter
hide its inner data structure (other object are interested by a service not how the service is built, this can also include optimisation)
preserve its integrity in every state (changing other fields if required)
it will use getter to
format data in output as desired by the client
control a sequence of services (for instance it will provide data if and only if a connection has been established)
相关文章推荐
- defineGetter and defineSetter in Firefox
- Let Us Fuck Up Getter And Setter (1)- Java Object Properties Copy
- [Ramda] Getter and Setter in Ramda & lens
- XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application
- Why getter and setter methods are evil
- java.lang.Object in Java and System.Object class in C#
- Data Structures and Algorithms in Java
- Concurrent and Distributed Computing in Java
- as3——跳出方法,getter and setter,子类实施超类的译本
- 啃"创建线程"一章的疑难解析: 通读Concurrent Programming in Java: Design Principles and Patterns (2)
- 说Hand-over-hand lock: 通读Concurrent Programming in Java: Design Principles and Patterns (1)
- Java 设计模式 动态调用 JavaBean 的 setter 和 getter
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- The longest class name in Java and .NET Framework
- Concurrent Programming in Java(TM): Design Principles and Pattern
- Exceptions in Java and C#
- s1(java)项目实战(固定资产管理)(asset_InAndOut )
- Fundamentals of OOP and Data Structures in Java
- Concurrent and Real-Time Programming in Java
- Java in Science: Data Interpolation and Extrapolation Using Numerical Methods of Polynomial Fittings