anonymous-class

What is an anonymous inner class?

守給你的承諾、 提交于 2019-11-27 08:33:07
问题 When I tried to do some sample on an abstract class in Java I accidentally got some thing like anonymous inner class in Eclipse. I have pasted the piece of code below. I don't understand how the abstract class is related to anonymous class. package com.Demo; abstract class OuterClass { abstract void OuterClassMethod(); } public abstract class InnerClass extends OuterClass { public static void main(String[] args) { InnerClass myInnerClass = new InnerClass() { @Override void OuterClassMethod()

abstract class and anonymous class [duplicate]

你。 提交于 2019-11-27 08:20:53
问题 This question already has an answer here: Creating the instance of abstract class or anonymous class 8 answers abstract class Two { Two() { System.out.println("Two()"); } Two(String s) { System.out.println("Two(String"); } abstract int display(); } class One { public Two two(String s) { return new Two() { public int display() { System.out.println("display()"); return 1; } }; } } class Ajay { public static void main(String ...strings ){ One one=new One(); Two two=one.two("ajay"); System.out

C# equivalent of creating anonymous class that implements an interface

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 07:55:12
问题 I've recently started using C#, and I wanted to find an equivalent method to this. I do not know what this is called, so I will simply show you by code. With Java, I was able to create an interface like so: public interface Event { public void execute(); } And pass this interface in a method's parameter like so: public class TestEvent { ArrayList<Event> eventList = new ArrayList<Event>(); public void addEvent(Event event){ eventList.add(event); } public void simulateEvent(){ addEvent(new

NotSerializableException on anonymous class

六月ゝ 毕业季﹏ 提交于 2019-11-27 05:24:36
I have an interface for filtering items: public interface KeyValFilter extends Serializable { public static final long serialVersionUID = 7069537470113689475L; public boolean acceptKey(String iKey, Iterable<String> iValues); public boolean acceptValue(String iKey, String value); } and a class containing a member of type KeyValFilter . public class KeyValFilterCollector extends KeyValCollectorSkeleton { private static final long serialVersionUID = -3364382369044221888L; KeyValFilter filter; public KeyValFilterCollector(KeyValFilter filter) { this.filter=filter; } } When I try to initiate the

Do anonymous classes *always* maintain a reference to their enclosing instance?

天涯浪子 提交于 2019-11-27 03:57:42
I'm working with some code where one object, "foo", is creating another object, "bar", and passing it a Callable . After this foo will return bar, and then I want foo to become unreachable (ie: available for garbage collection). My initial thought was to just create the Callable anonymously. eg: class Foo { ... public Bar createBar() { final int arg1 = ... final int arg2 = ... final int arg3 = ... return new Callable<Baz>() { @Override public Baz call() { return new Baz(arg1, arg2, arg3); } }; } } It occurred to me that this might not actually work as desired, however, as an inner class

Anonymous class in swift

五迷三道 提交于 2019-11-27 03:03:04
问题 Is there an equivalent syntax or technique for Anonymous class in Swift? Just for clarification Anonymous class in Java example here - http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html Thanks! 回答1: There is no equivalent syntax, as far as I know. Regarding equivalent techniques, theoretically you could use closures and define structs and classes inside them. Sadly, I can't get this to work in a playground or project without making it crash. Most likely this isn't ready

How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?

醉酒当歌 提交于 2019-11-27 02:52:34
问题 I woud like to write a JUnit test to verify that the code below uses a 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); } }; (FilterFactory is an interface.) My test thus far looks like this: @Test public void testBZIP2_FactoryUsesBufferedInputStream() throws

Calling newly defined method from anonymous class

房东的猫 提交于 2019-11-26 23:13:30
问题 I instantiated an object of an anonymous class to which I added a new method. Date date = new Date() { public void someMethod() {} } I am wondering if it is possible to call this method from outside somehow similar to: date.someMethod(); 回答1: Good question. Answer is No. You cannot directly call date.someMethod(); Let's understand first what is this. Date date = new Date() { ... }; Above is anonymous(have no name) sub-class which is extending Date class. When you see the code like, Runnable r

Is usage of anonymous classes in Java considered bad style or good?

限于喜欢 提交于 2019-11-26 22:47:25
问题 I know anonymous classes save typing when it comes to implementing Listener and similar stuff. They try to be a replacement for some usages of closures. But what does the community think about the value of this language-feature? Does it make sense and do you use it regularly? Does it make the code clearer, more understandable and more maintainable? Or do anonymous classes make the code less readable? What is your opinion, and please have examples/arguments handy to support your opinion? 回答1:

Setting outer variable from anonymous inner class

醉酒当歌 提交于 2019-11-26 22:09:53
Is there any way to access caller-scoped variables from an anonymous inner class in Java? Here's the sample code to understand what I need: public Long getNumber(final String type, final String refNumber, final Long year) throws ServiceException { Long result = null; try { Session session = PersistenceHelper.getSession(); session.doWork(new Work() { public void execute(Connection conn) throws SQLException { CallableStatement st = conn.prepareCall("{ CALL PACKAGE.procedure(?, ?, ?, ?) }"); st.setString(1, type); st.setString(2, refNumber); st.setLong(3, year); st.registerOutParameter(4,