简体   繁体   English

为什么我不能在类中声明字符串?

[英]Why can't I declare a string in a class?

public class Account {

    public int pin;
    public int accountnumber;
    public string accounttype;
    public double balance;
}

It tells me string cannot be resolved as a type. 它告诉我不能将字符串解析为类型。

它是大写的S String

Try 尝试

public String accounttype;

The others ( int , double ) are so called primitive data types where String is like Account "just another class". 其他类型( intdouble )称为原始数据类型,其中String类似于Account “只是另一个类”。
int has an "equivalent" (yeah, I know) ( Integer ), that is like String int有一个“等价”(是的,我知道)( Integer ),就像String

String is not a primitive type. 字符串不是原始类型。 In Java, objects/classes naming convention would have the first letter capitalized. 在Java中,对象/类的命名约定将首字母大写。

You know it is an object because it has methods and it is basically an array of characters. 您知道它是一个对象,因为它具有方法,并且基本上是字符数组。

Also you should declare most Class member variables as private (or protected) unless there is a specific reason you need to make it public. 另外,除非有特殊原因需要将其公开,否则应将大多数Class成员变量声明为私有(或受保护)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM