Why strange naming convention of “AlertDialog.Builder” instead of “AlertDialogBuilder” in Android

前端 未结 4 1209
时光取名叫无心
时光取名叫无心 2021-02-08 06:13

Why not

AlertDialogBuilder builder = new  AlertDialogBuilder(this);
builder.setTitle(\"foo\");

instead of

AlertDialog.Builder          


        
4条回答
  •  生来不讨喜
    2021-02-08 06:53

    Builder is the static inner class inside the AlertDialog class. So to create a Builder class object, you need to call AlertDialog.Builder.

    As there is no class like AlertDialogBuilder so you cannot do that.

    If you want you can also use as like bellow.

    Builder builder = new Builder(this);
    builder.setTitle("foo");
    

    But to use like this you need to import the Builder class to your class like

    import android.app.AlertDialog.Builder;
    

    instead of just

    import android.app.AlertDialog;
    

    A simple example

    class A{
         static class B{}
    }
    

    you cannot use

    AB obj = new AB();
    

    you have to use

    A.B obj = new A.B();
    

    Hope you are clear now.

提交回复
热议问题