我创建了一个匿名类,其中我声明了一些变量和方法.我的java老师告诉我要把这些私有化.我没有看到改变修饰符有什么不同,因为无论如何这些变量和方法对于匿名类是私有的,所以我更喜欢根本没有修饰符.谁是对的,什么更有意义?请参阅下面的示例代码,其中我选择"map"和"convert"的修饰符而不是将其设为私有.
Collections.sort(list, new Comparator<String>(){
public int compare(String a, String b){
return convert(a).compareTo(convert(b));
}
Map<String, String> map = new HashMap<String, String>();
String convert(String s) {
String u = map.get(s);
if (u == null)
map.put(s, u = s.toUpperCase());
return u;
}
});
Run Code Online (Sandbox Code Playgroud) 我想问一下使用匿名类和命名内部类的好习惯是什么?
我正在编写一个Android应用程序,其中包含许多UI元素(按钮,文本字段等).对于他们中的许多人我需要一些听众,所以在onCreate应用程序中我有一堆非常小的匿名类,如:
someButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// do something...
}
}
);
Run Code Online (Sandbox Code Playgroud)
每个这样的匿名类都是5到20行大 - 足够小,非常适合来自Java™的Nutshell书中的推荐:
通常,在以下情况下,您应该考虑使用匿名类而不是本地类:
- 班级的身体很短.
- 只需要该类的一个实例.
- 该类在定义后立即使用.
- 该类的名称不会使您的代码更容易理解.
但问题是,IMO onCreate变得非常大,通过快速查看代码变得更加复杂.它仍然很容易理解,但太大了.
那么在这种情况下会有什么更好的练习 - 拥有一堆小的内部子类,其中每个子类都很好地分开,但只使用一次或更好使用匿名类代替?
您可能知道,有些人使用Enum of 1实例声明单例,因为JVM保证总是会有一个没有并发问题的实例来处理...
那么具有多个实例的枚举呢?我们可以说像Enum这样的东西是一种有序的单身人士共享一个共同的界面吗?为什么?
public enum EnumPriceType {
WITH_TAXES {
@Override
public float getPrice(float input) {
return input*1.20f;
}
public String getFormattedPrice(float input) {
return input*1.20f + " €";
}
},
WITHOUT_TAXES {
@Override
public float getPrice(float input) {
return input;
}
},
;
public abstract float getPrice(float input);
public static void main(String[] args) {
WITH_TAXES.getFormattedPrice(33f);
}
}
Run Code Online (Sandbox Code Playgroud)
在此代码中,为什么这不起作用:WITH_TAXES.getFormattedPrice(33f); 如果不通过公共接口无法调用公共方法,那么声明公共方法的兴趣是什么?我想这就是为什么我没有看到任何语法能够为Enum的一个实例声明一个接口.
编辑:
似乎枚举实例是一种特殊的匿名类.因此,我理解为什么你不能称之为该方法.
我的问题有点与:为什么匿名类不能实现一个接口(除了它可能已经实现的接口!)
我完全理解为什么我们不能这样做:
Vehicle veh = new Vehicle() {
public String getName() {
return "toto";
}
}; …Run Code Online (Sandbox Code Playgroud) 你有什么理由不能将匿名类序列化为Json吗?
例:
public class AnonymousTest
{
private Gson gson = new Gson();
public void goWild()
{
this.callBack(new Result()
{
public void loginResult(Result loginAttempt)
{
// Output null
System.out.println(this.gson.toJson(result));
}
});
}
public void callBack(Result result)
{
// Output null
System.out.println(this.gson.toJson(result));
result.loginResult(result);
}
public static void main(String[] args)
{
new AnonymousTest().goWild();
}
}
Run Code Online (Sandbox Code Playgroud)
刚开始吧:)
从PHP7开始,我们有匿名类.
我们怎么知道an $instance是一个匿名类的实例?
我想写一个JUnit测试来验证下面的代码使用BufferedInputStream:
public static final FilterFactory BZIP2_FACTORY = new FilterFactory() {
public InputStream makeFilter(InputStream in) {
// a lot of other code removed for clarity
BufferedInputStream buffer = new BufferedInputStream(in);
return new CBZip2InputStream(buffer);
}
};
Run Code Online (Sandbox Code Playgroud)
(FilterFactory是一个接口.)
到目前为止我的测试看起来像这样:
@Test
public void testBZIP2_FactoryUsesBufferedInputStream() throws Throwable {
InputStream in = mock(InputStream.class);
BufferedInputStream buffer = mock(BufferedInputStream.class);
CBZip2InputStream expected = mock(CBZip2InputStream.class);
PowerMockito.spy(InputHelper.BZIP2_FACTORY); // This line fails
whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);
assertEquals(expected, observed);
}
Run Code Online (Sandbox Code Playgroud)
对PowerMockito.spy的调用会引发此消息的异常:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class edu.gvsu.cis.kurmasz.io.InputHelper$1
Mockito can …Run Code Online (Sandbox Code Playgroud) 让我们假设某些类是不可访问的,但是该类生成的另一个匿名类是可访问的.可以通过垃圾收集器删除第一个吗?
例:
class Outer {
public Object getInner() {
return new Object() {};
}
}
...
Outer outer = new Outer();
Object inner = outer.getInner();
// Could the "outer" instance be removed here considering that "inner" is using below?
Run Code Online (Sandbox Code Playgroud) 我是Android开发人员的新手.我读了一些关于它的书.并且所有作者都强烈建议使用匿名类而不是类重新定义.
他们说
TextView txtTitle;
...
txtTitle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
Run Code Online (Sandbox Code Playgroud)
比...更好
txtTitle.setOnClickListener(new MyOnClickListener(position));
...
private class MyOnClickListener implements OnClickListener{
...
}
Run Code Online (Sandbox Code Playgroud)
谁能解释一下为什么?
Ofc,如果我将重新定义类用于许多不同的对象,这将是修改的问题.
但是如果我只将自己的类用于特定的对象,那么我的类的逻辑不会强烈改变,我可以使用它吗?或者我应该使用匿名课程?
我遇到了一个局部变量访问内部类的问题需要被声明为final.它来自方法createGrids() - >" squares[i][j] = 0;",我是一个需要声明为final的局部变量.我不知道为什么和我在字段中添加了final,但它不能正常工作.
import java.util.ArrayList;
import java.util.Random;
Run Code Online (Sandbox Code Playgroud)
//省略
public class Minesweeper{
private JFrame frame;
private int cols = 9;
private int rows = 9;
public static final int GRID_HEIGHT = 9;
public static final int GRID_WIDTH = 9;
final JButton[][] grids = new JButton[GRID_WIDTH][GRID_HEIGHT];
final int [][] squares = new int [GRID_WIDTH][GRID_HEIGHT];
private static int width = 500;
private static int heigth = 400;
private JPanel s;
private JPanel n;
private JPanel w;
private int mines …Run Code Online (Sandbox Code Playgroud) 你们中的任何人都知道如何在这些代码中获得效果
public function create(SomeInterface $obj)
{
$class = get_class($obj);
return new class extends $class {
//some magic here
}
}
Run Code Online (Sandbox Code Playgroud)
很明显,该代码在 PHP 中不起作用。
$obj可以是许多不同类的实例。我想获得一个扩展 $obj 的类的实例。(它将使我能够重载一些基本方法)