java - 你如何制作一个可变的类级别?

标签 java class variables getter

如何制作可变类级别?这个词是什么意思?我被告知为了在不同的类中使用字符串全名,我需要使变量类级别并添加一个 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;
        }

    }

}

最佳答案

变量是类级别的,因为它在类的范围内。但是,它具有 private 修饰符,因此没有其他类可以访问它。

这个方法是你想要给另一个类访问你的字段的方法:

public class SaxHandler extends DefaultHandler {

...
    private String  fullname;

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

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

关于java - 你如何制作一个可变的类级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384723/

相关文章:

java - 将带值的字符串与带占位符的字符串进行比较以从中查找值

c++ - 初始化类内 vector vector 的两个维度

Java - 动态加载类

javascript - 除了 class、id 和 text 之外,我还能如何在 jQuery 元素中传递变量?

javascript - 如何在 JavaScript 对象文字中使用变量作为键?

java - Spring Boot 自定义登录 : "login", 模板可能不存在,或者可能无法被任何配置的模板解析器访问

java - GetKey 方法在 dataSnapshot 中返回 null

java - 继承 BlockJUnit4ClassRunner 时的 Eclipse/JUnit "Test class not found"

javascript - 为什么箭头函数可以在 ReactJS 类上使用,但不能在纯 ES6 类上使用?

c - 将变量分配给数组位置 [C]