我有三节课
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner
{ //constructor
InheritInner(WithInner wi) {
wi.super();
}
}
这个例子取自Eckel在Java中的思想。我不明白为什么我们不能调用wi = new WithInner();而不是.super()?在调用wi.super()时,我们正在调用对象的默认构造函数,不是吗?
我对Java很陌生,但我在C#上工作了多年。所以我要把用Java编写的代码翻译成C#。一切都还好,但我想出了一个似乎很奇怪的密码。以下是代码:
//Java Version
public class ParentClass{
ArrayList<SomeType> values;
void MyFunction(){ /*some codes*/ }
//other codes
public class NestedClass {
public void handleMessage(SomeType val) {
我正在阅读Kotlin in action这本书,我问自己“使用构造函数引用创建类的实例”的目的是什么(如果有人对这本书感兴趣并将其放在家里,请参见第112页)。 下面是书中的代码示例: data class Person(val name: String, val age: Int)
val createPerson = ::Person
val p = createPerson("Alice", 29)
println(p) // Person(name=Alice, age=29) 我认为它看起来像一个工厂方法调用,但我不认为这是这里方法引用的(唯一)目的。
在具体说明我的问题之前,让我提供一些背景:我的主要编程语言是C++和Java。在使用C++时,我发现应用const正确性很重要,即声明如下所示的函数:
A::DoSomething( const B& arg ); // guarantees that arg is not modified
A::DoSomething() const; // guarantees that the object of type A is not modified
A::DoSomething( const B& arg ) const; // both of the above
事实上,我经
(Java问题)
如果我在内部类中引用一个字段,这是否会导致封闭类和内部类之间的循环依赖?
我怎样才能避免这种情况?
下面是一个示例:
public class Outer {
private Other o;
private Inner i;
public Outer() {
o = new Other();
i = new Inner() {
public void doSomething() {
o.foo();
}
};
}
}
我试图在下面的程序中了解构造函数是如何工作的。基本上有一个扩展Seeker类的Player类和一个包含Seeker对象的Game类,如下面的代码所示。
我的问题是,当Game类使用mySeeker = new Seeker( "Sally", this );实例化mySeeker时,this会调用玩家的构造函数吗?如果是的话,它把游戏的名字传到哪里去了?我在Player的构造函数中看到了一个Player,但是我不知道游戏myGame (在main方法中)是如何传递给Seeker的。
//游戏课
class Game {
Seeker mySeeker;
int g
我有三门课:
class Superclass...
public class Generic{
public Generic(Superclass s){...}
}
以及扩展超类的子类
class SubClass extends Superclass{
public void methodM(){
Generic g = new Generic(???);
...
}
}
在methodM中,我使用构造函数Generic,并希望传递对子类扩展的超类实例的引用。所以我不想做像new Generic(new Sup
我一直面临着一种我不太明白的奇怪现象。我有一个由其他几个类扩展的抽象类。抽象类是一种特殊的集合,它有一个适合它的嵌套抽象迭代器类。每个扩展抽象集合类的类都有一个nester迭代器类,它扩展了原始抽象迭代器。
抽象类如下所示:
public abstract class AbstractMultiCollection<T> {
public AbstractMultiCollection() {
...
}
MultiIterator<T> iterator();
public abstract class AbstractM
可能重复:
有人能帮助我理解如何使用getConstructor创建内部类的实例吗?
这就是我现在的处境。
import java.lang.reflect.*;
public class Outer{
public Outer(int i){
//things to do
}
public class Inner{
Class<?>[] type = new Class<?>[1];
Class<?> myClass;
public Inner(int i){
//stuff and code
}
public v
我有一个静态的内部类,其中我想使用外部类的实例变量。目前,我不得不使用这种格式的"Outerclass.this.instanceVariable",这看起来很奇怪,有什么更简单的方法来访问外部类的实例字段吗?
Public class Outer
{
private int x;
private int y;
private static class Inner implements Comparator<Point>
{
int xCoordinate = Outer.this.x; // any easier way to access ou
假设我有一个代码:
public static void main(String[] args) {
Animal r1 = new Animal(10,"Amanda");
House c = new House(100, r1);
r1.setAge(3);
House c2 = new House(101, r1);
System.out.println(c);
System.out.println(c2);
}
输出为
3 Amanda
3 Amanda
所以看起来java所做的就是在House中的Animal
我有一个受保护的方法(我不希望在超类或它的子类之外看到这个方法)。我想在复制构造函数中使用它,但我不能。代码:
public class Superclass {
protected HashMap<Object, Object> getData() {
return new HashMap<>();
}
}
public class Subclass extends Superclass {
public Subclass(Superclass abstractClass) {
init(abstractCla
我想知道是否有任何内部类涉及的构造函数。例如,考虑下面给出的代码片段
class MyOuter
{
private int x= 10;
class MyInner
{
void dostuff(){
System.out.println("The value of x is "+x);
}
}
}
在另一个java文件中,我为MyOuter和MyInner类创建了实例,如下所示
Class Program
{
public static void main(String [] args)
{
在我的项目中,我有一个扩展片段类的类A,类A包含两个Textviews。问题是我必须从另一个B类访问Tetview,有人能给我一个解决方案或提示如何去做吗?
样本:
class A extends Fragment {
public TextView view1;
public TextView view2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = in
我对java引用类型的创建有一个疑问。
假设我下面有一个类
public class DefaultRepositorySelector
implements RepositorySelector
{
final LoggerRepository repository;
public DefaultRepositorySelector(LoggerRepository repository)
{
this.repository = repository;
}
public LoggerRepository getLoggerRepository()
{
我想做一个getter,它不允许调用者编辑返回的对象。
以List为例(虽然我希望答案也适用于任何其他类型),这是返回和编辑属性的常用方法:
class MyClass {
private List<String> strings;
// to get the whole object
public List<String> getStrings() {
return this.strings;
}
// to alter the object
public void addString(String