简体   繁体   English

Java如何访问一个类中的一个类

[英]Java how to access a class within a class

Here is basically what I have: 这基本上是我所拥有的:

public class Game extends Activity{
    public class Work{
        public class Shuffle{
            *Class I need to access*
        }
    }
}

Here is the class I will be accessing Shuffle from: 这是我将从中访问Shuffle的课程:

public class Deck extends Game {
    public int shuffle() {
        //WHAT DO I NEED TO DECLARE HERE IN ORDER TO ACCESS Shuffle.getShuffle()?
        int[] shuffDeck = (MY SHUFFLE CLASS).getShuffle();
        int x = shuffDeck[i];
        String y = String.valueOf(x);
        i += 1;

        return x;
    }
}

What do I need to declare to be able to access Shuffle.getShuffle() in my Deck class? 我需要声明什么才能访问Deck类中的Shuffle.getShuffle()?

Now taking of Nested Classes its of 2 types : 现在采用嵌套类的2种类型:

1. Inner Classes (Non-static) 1. Inner Classes (非静态)

2. Top Level Classes (static) 2. Top Level Classes (静态)

- Inner Clas s (Non-static) has an Implicit Reference to its Outer Class (Enclosing Class). - Inner Clas (非静态) 对其 Outer Class (封闭类)具有隐式引用 To access an Inner Class from outside you need to access it using the Outer Class Object . 要从外部访问内部类,您需要使用外部类对象进行访问

Eg: 例如:

 public class Outer{


     class Inner{


     }

 }


public class Test{


   public static void main(String[] args){

         Outer o = new Outer();
         Outer.Inner i = o.new Inner();

    }
} 

- A Top-Level Class (static) is just like a separate class in the Outer Class. - Top-Level Class (静态)就像外部类中的单独类一样。 Top-level class needs to create an object of the Outer Class to access its Non-static members , but It can Access the Static methods and Static variables of the Outer class directly. Top-level需要创建一个外部类的对象来访问其非静态成员 ,但是它可以直接访问外部类的Static methods and Static variables

Eg: 例如:

public class Outer{


         class Inner{


         }

     }


    public class Test{


       public static void main(String[] args){


             Outer.Inner i = new Outer.Inner();

        }
    } 

You cant access Inner class methods, and fields, from outer classes, directly. 您不能直接从外部类访问内部类的方法和字段。 However you can access outer class methods, inside inner classes. 但是,您可以在内部类内部访问外部类方法。

To access inner classes, you need to create an object of inner class, than only you can access inner class fields. 要访问内部类,您需要创建一个内部类对象,而不是仅您可以访问内部类字段。 See nested classes for more clarifications: 有关更多说明,请参见嵌套类:

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

You are talking about nested classes, try to read also this article. 您正在谈论嵌套类,请尝试同时阅读本文。 Maybe it can help. 也许可以帮上忙。

http://en.wikibooks.org/wiki/Java_Programming/Nested_Classes http://en.wikibooks.org/wiki/Java_Programming/Nested_Classes

A nested class should exist only to server outer class. 嵌套类仅应存在于服务器外部类。 You should not expose inner classes to outside world. 您不应将内部类暴露给外界。

public class Game extends Activity{


public static class Shuffle
        {
            // provide shuffle method here which will use internal implementation of
            // shuffle.
        }

int[] shuffle()
    {
        // call inner class method from here. Also declare inner class as
        // static since I guess your inner class does not require instance
        // of outer class.
       return null;
    }

And access shuffle() method of Game using Object of Game 并使用游戏对象访问游戏的shuffle()方法

   int[] shuffDeck = (Game object).getShuffle();
   public class Outer{
        class Inner{

       }
    }

    Outer.Inner art = (new Outer()).new Inner();

import android.content.Context;
import android.widget.Toast;

public class Outer{
    private Context context;
    public Outer(Context con) {
        context = con;
        String text = "Hello, I'm dbManager.";
        int duration = Toast.LENGTH_SHORT;
        Toast.makeText(context, text, duration ).show();
    }

    public class Inner{
        public Inner() {
            String text = "Hello, I'm «Art dbManager».";
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration ).show();
        }
    }
}

    Outer.Inner art = (new Outer(this)).new Inner();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM