Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建变量类级别?_Java_Class_Variables_Getter - Fatal编程技术网

Java 如何创建变量类级别?

Java 如何创建变量类级别?,java,class,variables,getter,Java,Class,Variables,Getter,如何创建变量类级别?这个词是什么意思?有人告诉我,为了在不同的类中使用字符串fullname,我需要将变量类设置为level并添加getter方法?有人能解释一下这个概念的含义以及它是如何正确实现的吗?下面列出了相关类中的代码 public class SaxHandler extends DefaultHandler { private String artifactIdTag = "close"; private String versionTag = "close";

如何创建变量类级别?这个词是什么意思?有人告诉我,为了在不同的类中使用字符串fullname,我需要将变量类设置为level并添加getter方法?有人能解释一下这个概念的含义以及它是如何正确实现的吗?下面列出了相关类中的代码

public class SaxHandler extends DefaultHandler {
    private String artifactIdTag = "close";
    private String versionTag = "close";

    private String  fullname;
    public String getFullname() {
         return fullname;
    }

    private boolean inDependency=false;

    private boolean inProperties= false;

    public void startElement(String uri, String localName,
            String qName, Attributes attributes)
            throws SAXException {


        if (qName.equalsIgnoreCase("properties")) {
            inProperties = true;
        }

        if (qName.equalsIgnoreCase("artifactId")) {
            artifactIdTag = "open";
        }
        if (qName.equalsIgnoreCase("version")) {
            versionTag = "open";

        }
        if (qName.equalsIgnoreCase("dependency")) {
            inDependency=true;
        }
        if (inProperties) {

            System.out.println("startElement property");
        }

    }

    private String artifact=null;
    private String version=null;
    public void characters(char ch[], int start, int length)
            throws SAXException {


        if (inProperties) {

            artifact = new String(ch, start, length);

            System.out.println("property characters: "+artifact );
        }


        if (artifactIdTag.equals("open")) {
            artifact = new String(ch, start, length);
            version=null;

            artifact = artifact.replace("-${org.springframework.version}", "");
            System.out.print("artifactId: " + artifact);

        }
        if (versionTag.equals("open")) {
            version = new String(ch, start, length) + ".jar";   

            version = version.replace("-${org.springframework.version}", "");


            String fullname=artifact +"-" +version;
            if (inDependency && !(artifact == null || version == null)){
            fullname = fullname.replace("-${org.springframework.version}", ""); 
            fullname = fullname.replace("-${spring.security.version}", ""); 
                System.out.printf("%-15s %n", fullname);
                FileNameStorage.add(fullname);
            }
        }

    }

    public void endElement(String uri, String localName,
            String qName) throws SAXException {

        if (qName.equalsIgnoreCase("artifactid")) {
            artifactIdTag = "close";
        }
        if (qName.equalsIgnoreCase("version")) {
            versionTag = "close";
        }
        if (qName.equalsIgnoreCase("dependency")) {
            inDependency=false;
        }
        if (qName.equalsIgnoreCase("properties")) {
            inProperties = false;
        }

    }

}

变量是类级别的,因为它在类的范围内。但是,它有私有修饰符,因此其他类无法访问它

此方法是您希望让另一个类访问您的字段的方法:

public class SaxHandler extends DefaultHandler {

...
    private String  fullname;

    public String getFullname()
    {
        return fullname;
    }
...
}

这个概念就是封装。您希望类的内部工作受到限制,这样它就不会被类之外的东西破坏。因此,您提供了一个getter方法来按值返回对字段的引用,这样就不能从外部修改该字段

班级级别可能意味着两件事之一:

  • 类中的一个静态变量,它是该类所有实例的一个变量实例
  • 类中的类成员变量,最终成为类的每个实例的一部分
    两者之间的区别在于是否使用静态修改器。如果将其用作类变量,如果不用作成员变量。您可以在getter方法上使用类似的修饰符来指示是否需要类的实例来访问变量。

    您写了这个吗?getter方法的调用是否与常规方法不同?没有区别