我编写了 Accessors 和 Mutators 方法,但仍然无法访问私有变量!为什么?

发布于 2024-12-26 14:58:48 字数 1123 浏览 2 评论 0原文

我用其私有变量编写了我的类,然后编写了访问这些变量所需的访问器和修改器方法,但是当我在编写主类后运行它时,这不起作用。为什么会发生这种情况?在这里检查我的代码:

public class DateTest{
    public static void main (String [] args){

        Date d1 = new Date();
        Date d2 = new Date();

        d1.month = "February ";
        d1.day = 13;
        d1.year = 1991;

        d2.month = "July";
        d2.day = 26;
        d2.year = 1990;

        d1.WriteOutput();
        d2.WriteOutput();
        }
    }


      class Date {

private String month;
private int day;
private int year;

public String getMonth(){
    return month;
                     }
public int getDay(){
    return day;
                   }
public int getYear(){

    return year;    }

public void setMonth(String m){
    if (month.length()>0)
        month = m;
                      }
public void setDay(int d){
    if (day>0)
     day = d;       }
public void setYear(int y){
     if (year>0)
     year = y;
                          }

   public void WriteOutput(){
    System.out.println("Month " + month + "Day "+ day + " year" + year);
    }
    }

请大家耐心等待我,我真的是一个“新手”程序员

I wrote my class with its private variables and then I wrote the accessor and mutator methods needed to access those variables, but that does not work when I run it after writing the main class. Why does that happening ?check my code here :

public class DateTest{
    public static void main (String [] args){

        Date d1 = new Date();
        Date d2 = new Date();

        d1.month = "February ";
        d1.day = 13;
        d1.year = 1991;

        d2.month = "July";
        d2.day = 26;
        d2.year = 1990;

        d1.WriteOutput();
        d2.WriteOutput();
        }
    }


      class Date {

private String month;
private int day;
private int year;

public String getMonth(){
    return month;
                     }
public int getDay(){
    return day;
                   }
public int getYear(){

    return year;    }

public void setMonth(String m){
    if (month.length()>0)
        month = m;
                      }
public void setDay(int d){
    if (day>0)
     day = d;       }
public void setYear(int y){
     if (year>0)
     year = y;
                          }

   public void WriteOutput(){
    System.out.println("Month " + month + "Day "+ day + " year" + year);
    }
    }

Please guys just be patient with me, I'm really a "novice" programmer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你的手,一向走下去 2025-01-02 14:58:48

应该调用访问器方法。就是这样。

d1.setMonth("February");
d1.setDay(13);

The accessor methods are supposed to be called. That's it.

d1.setMonth("February");
d1.setDay(13);
捎一片雪花 2025-01-02 14:58:48

Java 没有像 C# 那样的语法糖,并且不允许您从 object.property 进行调用,即使您已经提供了访问方法。属性纯粹是一种设计模式,并不反映在语言本身的语法中。

您需要显式调用它们,例如 d1.setMonth("February ");String val = d1.getMonth();

Java has no syntactic sugars like C# and won't allow you to do calls in from object.property even though you have provided the access methods. Properties are purely a design pattern and are not refleted in syntax of a language itself.

You need to call them explicitly like d1.setMonth("February "); and String val = d1.getMonth();.

半枫 2025-01-02 14:58:48

始终使用 setter 和 getter 来访问私有变量。

Always use setters and getters to access the private variables.

终弃我 2025-01-02 14:58:48

私有成员只能在同一个类的成员内部直接访问。 DateTest 是另一个类,因此下面的代码是不可能的

d1.month = "February ";
        d1.day = 13;
        d1.year = 1991;

        d2.month = "July";
        d2.day = 26;
        d2.year = 1990;

将上面的代码替换为使用相应的 setter 方法。

private members can be directly accessed only within the members of the same class. DateTest is another class and hence the below is not possible

d1.month = "February ";
        d1.day = 13;
        d1.year = 1991;

        d2.month = "July";
        d2.day = 26;
        d2.year = 1990;

Replace the above code with using corresponding setter methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文