getter和setter是如何工作的?

f4t66c6m  于 2021-07-06  发布在  Java
关注(0)|答案(6)|浏览(292)

我来自php世界。你能解释一下什么是能手和二传手吗?能举几个例子吗?

qltillow

qltillow1#

您可能还想阅读“为什么getter和setter方法是邪恶的”:
尽管getter/setter方法在java中很常见,但它们并不特别面向对象(oo)。事实上,它们会破坏代码的可维护性。此外,大量getter和setter方法的存在是一个危险信号,表明从oo的Angular 来看,程序不一定设计得很好。
本文解释了为什么不应该使用getter和setter(以及何时可以使用它们),并提出了一种帮助您摆脱getter/setter思维的设计方法。

oalqel3c

oalqel3c2#

class Clock {  
        String time;  

        void setTime (String t) {  
           time = t;  
        }  

        String getTime() {  
           return time;  
        }  
}  

class ClockTestDrive {  
   public static void main (String [] args) {  
   Clock c = new Clock;  

   c.setTime("12345")  
   String tod = c.getTime();  
   System.out.println(time: " + tod);  
 }
}

当你运行程序时,程序从主电源开始,
对象c已创建
功能 setTime() 由对象c调用
变量 time 设置为传递的值
功能 getTime() 由对象c调用
时间回来了
它将传递给 tod 以及 tod 打印出来

xtfmy6hx

xtfmy6hx3#

这并不需要教程。了解封装

private String myField; //"private" means access to this is restricted

public String getMyField()
{
     //include validation, logic, logging or whatever you like here
    return this.myField;
}
public void setMyField(String value)
{
     //include more logic
     this.myField = value;
}
iqxoj9l9

iqxoj9l94#

在java中,getter和setter是完全普通的函数。唯一能让他们成为能手或二传手的是传统。foo的getter称为getfoo,setter称为setfoo。对于布尔型,getter称为isfoo。它们还必须有一个特定的声明,如“name”的getter和setter示例所示:

class Dummy
{
    private String name;

    public Dummy() {}

    public Dummy(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

使用getter和setter而不是公开成员的原因是,这样可以在不更改接口的情况下更改实现。而且,许多使用反射来检查对象的工具和工具箱只接受具有getter和setter的对象。例如,javabean必须具有getter和setter以及一些其他需求。

3xiyfsfu

3xiyfsfu5#

下面是一个例子来解释在java中使用getter和setter的最简单方法。可以用一种更直接的方法来实现这一点,但是getter和setter有一些特殊的地方,那就是在继承的子类中使用父类的私有成员时。您可以通过使用getter和setter使之成为可能。

package stackoverflow;

    public class StackoverFlow 

    {

        private int x;

        public int getX()
        {
            return x;
        }

        public int setX(int x)
        {
          return  this.x = x;
        }
         public void showX()
         {
             System.out.println("value of x  "+x);
         }

        public static void main(String[] args) {

            StackoverFlow sto = new StackoverFlow();
            sto.setX(10);
            sto.getX();
            sto.showX();
        }

    }
k3fezbri

k3fezbri6#

1. 最能干的人是聪明的。

以下是来自mozilla的javascript示例:

var o = { a:0 } // `o` is now a basic object

Object.defineProperty(o, "b", { 
    get: function () { 
        return this.a + 1; 
    } 
});

console.log(o.b) // Runs the getter, which yields a + 1 (which is 1)

我经常用这些因为它们很棒。当我喜欢我的编码+动画时,我会用它。例如,创建一个setter来处理 Number 在你的网页上显示这个数字。使用setter时,它使用tweener将旧的数字设置为新的数字。如果最初的数字是0,你把它设置为10,那么你会看到数字从0快速翻转到10,比如说,半秒钟。用户喜欢这个东西,而且很有趣。

2. php中的getter/setter

sof示例

<?php
class MyClass {
  private $firstField;
  private $secondField;

  public function __get($property) {
    if (property_exists($this, $property)) {
      return $this->$property;
    }
  }

  public function __set($property, $value) {
    if (property_exists($this, $property)) {
      $this->$property = $value;
    }

    return $this;
  }
}
?>

城市:
https://developer.mozilla.org/en-us/docs/web/javascript/reference/functions/get
http://tweener.ivank.net/
盖特和塞特?

相关问题