简体   繁体   中英

how to pass the reference of private final class

I want to pass the reference of private final class into the method of another class please help me. Thanks

This is the class which I declared in a separate class file and I want to use it

private final class Mute extends BooleanControl {
        private Mute() {
            super(javax.sound.sampled.BooleanControl.Type.MUTE, false, "True", "False");
        }

In this method

public class BaseCameraActivity extends AppCompatActivity {

 protected void onCreateActivity() {

findViewById(R.id.btn_mute).setOnClickListener(v -> {
        //here i want to use the private class mute
    });

As others have said, the private class can only be used within its definition file. There must be some occurrence of new for that class if it used at all. I will suppose there is indeed a public class in that file (which I call Container )

So search for code that says : new Mute() in the file hosting the class Mute. Ideally you will find a function in the file that does something like :

public BooleanControl createMute() { return new Mute() ; }

BooleanControl (or maybe some interface that BooleanControl itself implements) is an (abstract) reference to your (concrete) "Mute" object that you can use and pass around, and which is not private itself.

ie in your client code you get :

Container c = new Container();
BooleanControl  bc = c.createMute();
... use bc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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