我们先来看看set和get这两个词的表面意思,set是设置的意思,而get是获取的意思,顾名思义,这两个方法是对数据进行设置和获取用的。而且,在类中使用set和get方法时,都是在set和get后面跟上一些特定的词来形成特定意思的方法名,比如setage()和getage(),表示设置年龄和获取年龄。
然后我们来了解一下JAVA面向对象编程中的封闭性和安全性。封闭性即对类中的域变量进行封闭操作,即用private来修饰他们,如此一来其他类则不能对该变量访问。这样我们就将这些变量封闭在了类内部,这样就提高了数据的安全性,当我们想要操作这些域变量怎么办呢?我们可以通过两种方法,第一中即通过public方式的构造器(或称构造函数),对象一实例化就对该变量赋值。第二种就是通过上面提到的set和get方法,这里我举一个特定的例子,我定义一个Person类,该类中有name、age这两个私有域变量,然后我定义setname()、getname()、setage()、getage()这四个方法,通过这四个方法来实现对name和age的操作。这样一来,我不用直接对Person类中的域变量操作,而是通过set和get方法间接地操作这些变量,这样就能提高域变量的安全性,同时又保证了域变量的封装型。
最后说说set和get方法的使用场景,一般来说set和get方法都是对私有域变量进行操作的,所以大多数都是使用在包含特定属性的类实体中。
现在你明白set和get方法了吧,其实这两个方法只是类中的setxxx和getxxx方法的总称。
在今后的开发过程中,只要你细心体会,结合JAVA中的一些宏观的知识。set和get方法还是很容易理解的,或许你还会领会一些其他的JAVA中的重要理论知识。
- package cn.itcast.domain;
-
- import java.util.Date;
-
- public class User {
-
- private String username;
- private String password;
- private String gender;
- private Date birthday;
- private String email;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
-
- }
-
- User u = new User();
- u.setXXX();
- u.getXXX();
- 你想给他赋值就用u.setXXX(); 取这个类的对象的某个值 就get
------------------------------------------------------------------------------------------------------------
---
在java中, 进行封装之后,不能公开使用public进行调用属性,使用private和default 等,进行封装,之后,在需要调用属性的时候,可以使用set() get() 方法进行调用,
set()方法和构造方法不一样,构造方法是给变量赋初始值,set方法是赋值。
上述图片需求代码的执行结果
-
- D:\workspace\easy\prc01>java -classpath .;classes com.ab.xm.Forobject
- 小明骑着一辆价值为1880.0ofo小黄车,去长江路199号浦口外国语中学考试,
- 小明做了选择0
- 小明做了选择1
- 小明做了选择2
- 小明做了选择3
- 小明做了选择4
- 小明做了判断0
- 小明做了判断1
- 小明做了判断2
- 小明做了判断3
- 小明做了判断4
- 小明做了判断5
- 小明做了判断6
- 小明做了判断7
- 小明做了判断8
- 小明做了判断9
代码如下
- package com.ab.xm.tools;
- import com.ab.xm.Person;
- import com.ab.xm.edu.School;
- public class Bike{
- // double price;
- // String color;
- // String brand;
- // String username;
-
- private double price;
- private String color;
- private String brand;
- private String username;
-
- public Bike() {
-
- }
-
- // public Bike(double price, String brand, String color) {
- // this.price = price;
- // this.color = color;
- // this.brand = brand;
- // }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public double getPrice() {
- return this.price;
- }
-
- public void setBrand(String brand) {
- this.brand = brand;
- }
-
- public String getBrand() {
- return brand;
- }
-
- public void setColor(String color) {
- this.color = color;
- }
-
- public String getColor() {
- return color;
- }
-
- public void gotoschool(Person person, School school) {
- // School school = new School();
- System.out.println(
- person.getName()
- + "骑着一辆价值为"
- + price
- + brand
- + color
- + ",去"
- + school.getAddress()
- + school.getName()
- + "考试,"
- );
-
- // public void gotoschool(String username, School school) {
- // System.out.println(username
- // + "骑着一辆价值为"
- // + price
- // + brand
- // + color
- // + ",去"
- // + school.address
- // + school.name
- // + "考试,"
- // );
-
- }
-
-
- }
- package com.ab.xm;
- import com.ab.xm.edu.School;
- import com.ab.xm.tools.Bike;
- import com.ab.xm.Person;
- import com.ab.xm.edu.Paper;
-
- // import com.ab.xm.edu.*;
- // import com.ab.xm.tools.*;
-
- public class Forobject{
- public static void main(String[] args) {
- // Person p = new Person("小明", 15, "天润城十六街区");
- // Bike bike = new Bike(1880, "ofo", "小黄车");
- // Paper paper = new Paper(5, 10);
-
- Person person = new Person();
- person.setAge(15);
- person.setName("小明");
- person.setAddress("天润城十六街区");
-
- School school = new School();
- school.setName("浦口外国语中学");
- school.setAddress("长江路199号");
-
- Paper paper = new Paper();
- paper.setChoice(5);
- paper.setJuge(10);
-
- Bike bike = new Bike();
- bike.setPrice(1880);
- bike.setBrand("ofo");
- bike.setColor("小黄车");
- person.goschool(bike,school);
- person.goexam(paper);
- // School school = new School("浦口外国语中学", "长江路199号");
- // p.goschool(bike, school);
- // p.goexam(paper);
- // System.out.println();
-
- // }
-
- }
-
- }
- package com.ab.xm.edu;
- import com.ab.xm.Person;
- import com.ab.xm.tools.Bike;
-
-
- public class Paper{
- // int choice;
- // int juge;
- private int choice;
- private int juge;
-
- public Paper() {
-
- }
-
- public void setChoice(int choice) {
- this.choice = choice;
- }
- public int getChoice() {
- return choice;
- }
-
- public void setJuge(int juge) {
- this.juge = juge;
- }
- public int getJuge() {
- return juge;
- }
- // public Paper(int choice, int juge) {
- // this.choice = choice;
- // this.juge = juge;
- // }
- public void exam(Person person) {
- for(int i = 0; i < choice; i++) {
- System.out.println(person.getName() + "做了" + "选择" + i);
- }
-
- for(int i = 0; i < juge; i++) {
- System.out.println(person.getName() + "做了" + "判断" + i);
- }
-
- // public void exam(Person person) {
- // for(int i = 0; i < choice; i++) {
- // System.out.println(person.name + "做了" + "选择" + i);
- // }
-
- // for(int i = 0; i < juge; i++) {
- // System.out.println(person.name + "做了" + "判断" + i);
- // }
- }
- }
- package com.ab.xm;
- import com.ab.xm.edu.School;
- import com.ab.xm.tools.Bike;
- import com.ab.xm.edu.Paper;
- public class Person{
- // String name;
- // int age;
- // String address;
- // School school;
- // Bike bike;
- private String name;
- private int age;
- private String address;
- private School school;
- private Bike bike;
-
- public Person() {
-
- }
- public void setName(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public int getAge() {
- return age;
- }
-
- public Person(String name, int age, String address) {
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
-
-
-
- // public Person(String name, int age, String address) {
- // this.name = name;
- // this.age = age;
- // this.address = address;
- // }
- public void goschool(Bike bike, School school){
- bike.gotoschool(this, school);
- }
-
- public void goexam(Paper paper){
- paper.exam(this);
- }
-
- }
- package com.ab.xm.edu;
- public class School{
- // String name;
- // String address;
-
- private String name;
- private String address;
-
- public School(){
-
- }
-
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
- public String getAddress() {
- return address;
- }
- // public School(String name, String address) {
- // this.name = name;
- // this.address = address;
- // }
-
- }
编译用 javac -d . -encoding utf-8 *.java (注意,-d 后面的 “ . ”,前后各空一位)
-d <目录> 指定放置生成的类文件的位置
执行用 java -classpath .; classes com.ab.xm.Forobject (注意,-classpath .;-----这里,空一格再英文 点 )