在Java中访问类的字段 [英] accessing fields of a class in Java

查看:45
本文介绍了在Java中访问类的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java完全陌生.

我正在练习一个关于某人吃水果的代码.我有3节课

水果类:

 公共类Fruit {字符串fruitname ="grapes";} 

人员类别:

 公共类Person {无效吃(水果f){System.out.println(人在吃" + f.fruitname);//我该怎么办f.fruitname}} 

测试类别:

 公共类TestFruit {公共静态void main(String [] args){人p =新的Person();//人对象水果f =新的Fruit();//水果对象p.eat(f);}//人类的进餐方法} 

输出:

 人在吃葡萄 

要访问类的字段,将创建该类的对象.

我的问题是:

Person 类中,如何访问 Fruit 类的 fruitname 字段(即编写 f.fruitname )而没有实例化 Person 类中的 Fruit 类?

fruitname Fruit 类的数据成员,实例成员在创建对象之前不存在.

我刚刚开始学习Java,并且一直被困在这里.请帮助我理解.

解决方案

您正在做的事情行不通,因为您没有将成员字段声明为 public :

  public字符串fruitname =葡萄"; 

只有这样,您才可以编译它:

  System.out.println(人在吃东西" + f.fruitname); 

请注意,在Java字段中,默认情况下默认为 private 包(另请参见).这意味着字段可以 private ,但是在这种情况下,您只能在位于同一包中的类中访问此字段.


但是,通常情况下,会创建如下的getter和setter方法:

 公共类Fruit {private String fruitname =葡萄";公共字符串getFruitname(){返回水果名称;}public void setFruitname(String fruitname){this.fruitname =水果名;}} 

,这将允许您像这样访问类成员 fruitname :

 公共类Person {公众用餐(水果f){System.out.println(人在吃东西" + f.getFruitname());}} 

根据您的IDE,您也许可以右键单击该字段(或类中的某个地方),然后找到类似 Generate ..>的内容.吸气剂塞特犬(Setters),这使整个动作不那么烦人.

I am completely new to Java.

I was practicing a code about a person eating some fruit. I have 3 classes

Fruit Class:

public class Fruit {
    String fruitname = "grapes";
}

Person Class:

public class Person {
    void eat(Fruit f) {
        System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
    }
}

Test Class:

public class TestFruit {
    public static void main(String[] args) {
        Person p = new Person(); // person object
        Fruit f = new Fruit(); // fruit object
        p.eat(f);
    } // eat method of person class
}

output:

person is eating grapes

For accessing fields of a class, Object of that class is created.

My question is:

In Person class, how can I access fruitname field of Fruit class (i.e., writing f.fruitname) without instantiating Fruit class in Person class?

fruitname is a data member of Fruit class and instance member don't exist until object is created.

I have just started learning Java, and I am stuck here. Please help me to understand.

解决方案

What you're doing does not work because you're not declaring the member field as public:

public String fruitname = "grapes";

Only then you can even compile this:

System.out.println("person is eating " + f.fruitname);

Note that in Java fields are package private per default (see also). This means that the field can be private but in this case you can only access this field in classes which reside in the same package.


However, in general one creates getter and setter methods like this:

public class Fruit {

    private String fruitname = "grapes";

    public String getFruitname() {
        return fruitname;
    }

    public void setFruitname(String fruitname) {
        this.fruitname = fruitname;
    }
}

which will allow you to access the class member fruitname like this:

public class Person {
    public void eat(Fruit f) {
        System.out.println("person is eating " + f.getFruitname());
    }
}

Depending on your IDE you might be able to right click the field (or somewhere in the class) and find something like Generate.. > Getters & Setters which makes the whole act less annoying.

这篇关于在Java中访问类的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆