在Java中创建一个空的POJO类是否明智?

huangapple go评论70阅读模式
英文:

Is it advisable to create an empty POJO class in Java?

问题

假设我有一个超类A,它有以下字段:

class A {
    String name; 
    int age;

    void setName(String name);
    String getName();

    void setAge(int age);
    int getAge();
}

我有多个继承自A的子类,并且在子类中添加了更多字段以及相应的getter和setter方法。

现在有另一个类,称为B,它需要使用已经在类A中提供的name和age字段。那么,我应该继续创建一个没有任何字段的B类,并使其简单地继承类A呢,还是应该直接使用类A呢?

class B extends A {}

附注:我正在使用泛型,在直接使用超类A时会收到警告,但功能正常。请给予建议。

英文:

Suppose I have a superclass A and it has fields

class A {
    String name; 
    int age;
    setName(String name);
    getName();

    setAge(int age);
    getAge()
}

I have multiple classes that extend A and add more fields along with the getters and setters.
Now there is another class, say B, which requires name and age, which is already provided by class A.
So should I go ahead and create class B without any field and simply it extends class A, or should I directly use class A?

class B extends A {}

P.S - I am using generics, which gives me a warning when I directly use superclass A, but the functionality is working fine. Please suggest.

答案1

得分: 1

大多数情况下,如果只是创建一个没有自己状态的类,那么设计通常不会合适和合理,但如果 A 是一个抽象类,即你希望限制用户创建 A 的实例,因此将其标记为抽象,然后通过创建 B,你正在创建 A 的实现。以下是相应示例:

abstract class A{
    protected String name;
    protected int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

class B extends A{}

另外,如果你想要在运行时使代码更加动态,只需将父类字段用于函数,那么可能可以这样做:

abstract class A{  // 你可以完全移除 'abstract' 并且不创建 B 类
    protected String name;
    protected int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

class B extends A{}

class C extends A{
    protected String location;

    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
}

现在看一下下面的方法:

public static <T extends A> void printName(List<T> list) {
    for (T t : list) {
        System.out.println(t.getName());
    }
}

这适用于 List<B>List<C>

英文:

Mostly the design wont be proper and justified if u just create a Class that do not have its own state, but yes it make sense if

A is abstract class i.e. you want to restrict the users to create an instance of A hence mark it abstract , then by creating B you are creating an implementation of A.
below is the example for the same

abstract class A{
	protected String name;
	protected int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

class B extends A{}

also to make code more dynamic at runtime if u want to just use the parent class fields into ur function then probably u can do this

abstract class A{  // you can altogether remove &#39;abstract&#39; and not create a B class
	protected String name;
	protected int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

class B extends A{}

class C extends A{
	protected String location;

	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
}

now see the below method

public static &lt;T extends A&gt; void printName(List&lt;T&gt; list) {
		for (T t : list) {
			System.out.println(t.getName());
		}
	}

this qualifies for List&lt;B&gt;, List&lt;C&gt;

答案2

得分: 0

确实没有任何理由,我能想到的,需要扩展一个类却不改变任何内容。也许你觉得将来会需要它。这违反了【你不会需要它(YAGNI)】原则。
只需使用A类。在需要时再进行更改。

英文:

There is really no reason, that I can think of, where you need to extend a class without changing anything. Maybe you feel that you will need it in the future. This violates the YAGNI principal.
Just use Class A. You can make changes when they are needed.

答案3

得分: 0

这真的没有意义。如果它给你一个警告,那可能有一个很好的原因。除非你有一个特殊的用例并且知道你在做什么,否则空类除了向你的项目添加无用的文件之外没有任何用处。

英文:

It really is pointless. If it gives you a warning, there is probably a good reason for it. Unless you have a niche use case and know what you are doing, the empty class serves no purpose other than adding useless files to your project

huangapple
  • 本文由 发表于 2020年9月2日 00:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63691888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定