我有一个父类,它定义了一个chainer方法的集合(返回"this"的方法).我想定义多个子类,这些子类包含自己的chainer方法,但也"覆盖"父方法,以便返回子类的实例而不是父类.
我不想在每个子类中重复相同的方法,这就是为什么我有一个包含所有子类共享的方法的父类.谢谢.
class Chain {
public Chain foo(String s){
...
return this;
}
}
class ChainChild extends Chain {
//I don't want to add a "foo" method to each child class
/*
public ChildChain foo(String s){
...
return this;
}
*/
public ChainChild bar(boolean b){
...
return this;
}
}
ChainChild child = new ChainChild();
child.foo().bar(); //compile error: foo() returns a "Chain" object which does not define the bar() method.
Run Code Online (Sandbox Code Playgroud) 我构建了一个类(例如一个类来制作简单的动画):
public class myAnimations {
private Animation animation;
private ImageView imageView;
public myAnimations(ImageView img) {
super();
this.imageView = img;
}
public void rotate(int duration, int repeat) {
animation = new RotateAnimation(0.0f, 360.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setRepeatCount(repeat);
animation.setDuration(duration);
}
public void play() {
imageView.startAnimation(animation);
}
public void stop() {
animation.setRepeatCount(0);
}
}
Run Code Online (Sandbox Code Playgroud)
我只是可以这样使用它:
ImageView myImage = (ImageView) findViewById(R.id.my_image);
myAnimations animation = new myAnimations(myImage);
animation.rotate(1000, 10);
animation.play(); //from this way…
Run Code Online (Sandbox Code Playgroud)
但如果我想能够像这样使用它:
ImageView myImage = (ImageView) findViewById(R.id.my_image);
myAnimations animation = …
Run Code Online (Sandbox Code Playgroud) 我注意到,在使用扫描仪时,如果我想设置分隔符或区域设置等属性,则执行此操作的方法会返回对象Scanner
本身:
public Scanner useDelimiter(String pattern) {
modCount++;
delimPattern = patternCache.forName(pattern);
return this;
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是,如果属性被更改(而不是创建一个新对象),为什么它会返回一个Scanner
对象而不是void
?这并不是说我必须将返回值存储在变量中 - 事实上,如果我尝试这样做,就像下面的代码一样,Eclipse 将给出以下消息Resource leak: 'lineScanner' is not closed at this location
:
Scanner scanner = new Scanner(new File("accounts.csv"));
String line;
while(scanner.hasNextLine()) {
line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner = lineScanner.useDelimiter(",");
...
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找名为Product的课程,其中包括以下内容:
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
public Product withName(String name) {
this.name = name;
return this;
}
Run Code Online (Sandbox Code Playgroud)
withName方法的目的是什么,它是如何使用的?为什么不这样做:
Product p = new Product();
p.setName("foo");
Run Code Online (Sandbox Code Playgroud) 我想创建一个包含 20 个字段的大型 Java 对象,大多数 IDE 都提供了一个生成函数,它允许我为对象的所有字段生成 getter 和 setter。
问题是,我是方法链的忠实粉丝,我在我所有的对象上都使用它,但我必须return this
在每个的末尾添加setters
,这不是很方便。
是否可以通过 IDE 的特殊插件生成设置器?
我在线学习Spring框架.但是,我对这个Java代码有疑问:
attributes.addFlashAttribute(ResultMessages.success().add(
ResultMessage.fromText("Created successfully!")));
return "redirect:/todo/list";
Run Code Online (Sandbox Code Playgroud)
我在想什么:是'Class.method.method'吗?如果是这样,怎么可能呢?
在使用Google Services库时,我在Android编程中看到了很多编码风格,他们在初始化类的实例后使用点来调用方法.
例如,假设我有一个Person类:
public class Person {
private String firstName;
private String lastName;
private String occupation;
private String primarySkill;
private String secondarySkill;
/* ******************
* CONSTRUCTOR
* ******************/
public Person(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
/* ******************
* MUTATORS
* ******************/
// firstName Setter:
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// lastName Setter:
public void setLastName(String lastName) {
this.lastName = lastName;
}
// occupation Adder:
public void addOccupation(String occupation) {
this.occupation = occupation;
}
// …
Run Code Online (Sandbox Code Playgroud)