安卓面试

Java面试题

2016-03-14  本文已影响157人  elon

Anonymous inner classes are somewhat limited compared to regular inheritance, because they can either extend a class or implement an interface, but not both. And if you do implement an interface, you can only implement one.

If you don’t need a connection between the inner-class object and the outerclass object, then you can make the inner class static. This is commonly called a nested class. To understand the meaning of static when applied to inner classes, you must remember that the object of an ordinary inner class implicitly keeps a reference to the object of the enclosing class that created it. This is not true, however, when you say an inner class is static. A nested class means:You don’t need an outer-class object in order to create an object of a nested class. You can’t access a non-static outer-class object from an object of a nested class.
Nested classes are different from ordinary inner classes in another way, as well. Fields and methods in ordinary inner classes can only be at the outer level of a class, so ordinary inner classes cannot have static data, static fields, or nested classes. However, nested classes can have all of these

基本数据类型 大小(bit) 包装器类型
boolean - Boolean
byte 8 Byte
char(表示Unicode) 16 Character
short 16 Short
int 32 Integer
long 64 Long
float 32 Float
double 64 Double

Cannot switch on a value of type long. Only convertible int values, strings or enum variables are permitted

|修饰符|当前类|同一package|子孙类|其他package|
|:--|:--|:--|:--|
|public|√|√|√ |√|
|protected|√|√|√|×|
|friendly|√|√|×|×|
|private|√|×|×|×|

Let’s discuss overloading in detail. In websites, I generally see a overloaded truck as example for overloading. It symbolizes that, adding more attributes, methods to a class and making it look bulkier is overloading. Please get it right, in fact when we use overloading for the outsiders view the class will look compact.

稍微延伸一下,overload表示:同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同)。注意:不能通过返回值类型、修饰符来重载。
override表示:子类中的方法可以与父类中的某个方法的名称和参数完全相同,通过子类创建的实例对象调用这个方法时,将调用子类中的定义方法,这相当于把父类中定义的那个完全相同的方法给覆盖了,这也是面向对象编程的多态性的一种表现。如果父类的方法是private类型,那么,子类则不存在覆盖的限制,相当于子类中增加了一个全新的方法。

父类或接口定义的引用变量可以指向子类或具体实现类的实例对象,而程序调用的方法在运行期才动态绑定,就是引用变量所指向的具体实例对象的方法,也就是内存里正在运行的那个对象的方法,而不是引用变量的类型中定义的方法。

Person类的实例是张三、李四,而Class的实例是JVM中的字节码,也就是Person.classAnimal.class

泛型的主要目标是提高 Java 程序的类型安全。通过知道使用泛型定义的变量的类型限制,编译器可以在一个高得多的程度上验证类型假设。

上一篇 下一篇

猜你喜欢

热点阅读