-8

So as part of my IT project I have to do a method class. I already did this but am finding trouble to complete it. When i checked the notes i found something about the get and set method. Can anyone help me pls by showing how the first one is made or just by telling what i should do?

public class MethodClassProject 
{  

Integer Id ;
String Brand ;
String Price ;
String Size ;
String Quantity ;
String Code ;
String Color ;
String Style ;

void setId (int userId)
{
    Id = userId ;
}
void setBrand (String userBrand)
{
    Brand = userBrand ;
}
4

2 回答 2

0

你肯定是在正确的轨道上,但这里有一些建议:

  • java中的变量名应该以小写字母开头
  • 注意你的缩进。虽然它对编译器来说并不重要,但当缩进不正确时,人类很难阅读。
  • 注意你的大括号,你提供的代码中缺少一个右大括号。
  • 查找如何获取return项目
  • this也可以在 Java 中查找关键字
于 2015-03-04T15:42:17.547 回答
0

阅读return关键字。getter 和 setter 方法的示例可以在getter 和 setter 如何工作?. setter 方法是一种在您的类中设置变量的方法。Getter 方法将变量提供给调用该方法的任何对象。

吸气剂:

在主要方法中:

String s = MethodClassProject.getBrand();

将设置String s为您的品牌名称。

二传手:

MethodProjectClass.setBrand("Diet Coke");

将品牌名称设置为健怡可乐。

一起:

class MainWrapper {
    public static void main(String[] args) {
        MethodProjectClass.setBrand("Diet Coke");
        System.out.println(MethodProjectClass.getBrand()); // Prints Diet Coke
    }
}

如果您希望在不更改输入值的情况下设置变量,我还建议您简单地公开您的变量。

我会把课堂上实际的getter方法留给你,否则你学不会。

于 2015-03-04T15:40:03.513 回答