从毕业到如今,笔试过好多场 也面了好多场, 一直想写一些东西给找工做的朋友,这篇文章结合我的经历讲解面试中的面向对象.里边有些实例我的以面试题的形式写出来,从程序角度去b帮助你们了解学习,最后但愿你们找个好工做.
html
Java面向对象的特征: 抽象、封装、继承、多态.程序员
抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解所有问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面:数据抽象和过程抽象.
面试
封装:封装就是对属性和方法的载体类,只能经过其提供的接口(方法)来访问,而把实现细节隐藏起来.也就是说,具体实现对程序员来讲是透明的,封装的好处在于对类内部的改变,不会影响到其余代码.函数
封装的作法:学习
1.私有属性(private修饰符修饰属性)。spa
2.提供public的读(getXX)写(setXX)方法。code
3.在构造中调用方法.全部的很是量属性基本都须要封装.htm
封装的好处:对象
1.隐藏类的实现细节blog
2.对全部用户提供统一的接口
3.加强执行效果.
4.易于维护和扩展
继承:继承是一种关系,逻辑上知足子类is a 父类的关系才使用继承.
子类继承父类的属性和非私有方法.不能继承父类的构造,继承关键字extends,类单继承,接口多继承.
在构造子类对象时,依次调用父类的构造(子类默认调用父类的无参构造.可使用super(参数列表)来调用指定的父类的含参构造)到Object为止.再调用子类自身的.
子类调用父类的构造时,父类的构造只能调用一个且必须写在子类构造的第一句.
【笔试题】: 类加载时的顺序问题:说出下面程序的执行结果
public class Test { public static void main(String[] args) { new B(); System.out.println("--------"); new B(); } } class A{ //静态代码块
static{ System.out.println("static code block in father"); } //代码块
{ System.out.println("code block in father"); } //构造
public A(){ System.out.println("construction of father"); } } class B extends A{ //静态代码块
static{ System.out.println("static code block in son"); } //代码块
{ System.out.println("code block in son"); } //构造
public B(){ System.out.println("construction of son"); } }
程序执行结构
static code block in father static code block in son code block in father construction of father code block in son construction of son -------- code block in father construction of father code block in son construction of son
多态:多态性是指容许不一样类的对象对同一消息做出响应。多态性包括参数化多态性和包含多态性。多态性语言具备灵活、抽象、行为共享、代码共享的优点,很好的解决了应用程序函数同名问题。
多态的类型
1.基本类型的多态(本质上是基本类型之间的自动类型转换)
Java语言中将8种数据类型都分别封装了一个类,这些类中封装了各基本数据类型的属性和基本运算.
基本类型自动转换为对应的封装类的操做叫作自动装箱操做,反之叫自动拆箱操做,自动装箱操做有一个自动装箱池(范围为-128~127).只要自动装箱的数在自动装箱池范围内,则直接去池中找数据.
public class TestAutoPool { public static void main(String[] args) { Integer num1 = 127; Integer num2 = 127; System.out.println(num1==num2);//true
Integer num3 = 128; Integer num4 = 128; System.out.println(num3==num4);//false
} }
Charater在5.0没有该自动装箱池,在6.0有自动装箱池.
byte、short、int、long有本身的自动装箱池,其余基本类型都没有该自动装箱池.
自动装箱不会出现错误,自动解箱在下面状况下会出错
Integer num = null; int i = num;//null不能自动解箱
【笔试题】:说出下面程序的执行结果
public class Test { public static void main(String[] args) { short a = 128; byte b = (byte)a; System.out.println("a=" + a + ",b=" + b); } }
程序运行结果:
a=128,b=-128
2.方法的多态(方法的重载、重写)
a)覆盖
父类继承过来的方法对子类不合适时子类能够改变该方法的实现,这种操做叫作方法的重写/覆盖(继承是重写的前提条件)
重写的要求:
<1>.方法名、参数列表和返回类型必须相同(5.0之后容许返回子类类型).
<2>.访问权限能够改可是不容许更小.(即子类的访问权限要么大于父类的要么相同,不容许小于父类的访问权限)
<3>.抛出的异常不能更大.
b)重载
方法的重载要求方法名必须相同,参数必须不一样(参数个数不一样、类型不一样、顺序不一样<(int,String)和(String,int)是不同的>).返回值类型能够相同能够不相同.
3.类或者接口的多态(父类的引用指向子类的对象):Person p = new Student();
父类的引用指向子类的对象就发生了多态.后果:
<1>.只能使用父类中方定义的属性和方法
<2>.子类中定义的不能直接使用
<3>.子类复写了父类的方法,此时调用状况根据方法是否static而不一样 [static(调用父类),非static(调用子类)].
<4>.若是想使用子类中定义的方法,能够强制类型转换(判断是否能够转换,用instanceof运算符来判断对象的类型)
【笔试题】:说出下面程序的执行结果
class A { int a = 1; static int b = 20; int c = 3; double d = 2.0; void show() { System.out.println("Class A: a=" + a + "\td=" + d + "\tc=" + c + "\tb=" + b); } void common(){ System.out.println("Class A: method common()"); } static void execute(){ System.out.println("Class A: method excute()"); } } class B extends A { float a = 3.0f; static int b = 30; int c = 5; String d = "Java program."; void show() { super.show(); System.out.println("Class B: a=" + a + "\td=" + d + "\tc=" + c + "\tb=" +b); } void common(){ System.out.println("Class B: method common()"); } static void execute(){ System.out.println("Class B: method execute()"); } public static void main(String[] args) { A a = new B(); a.show(); System.out.println("----------------------"); a.common(); System.out.println("----------------------"); a.execute(); } }
程序运行结果
Class A: a=1 d=2.0 c=3 b=20 Class B: a=3.0 d=Java program. c=5 b=30
---------------------- Class B: method common() ---------------------- Class A: method excute()
4.传参时的多态(基本类型的多态与类类型的多态混合使用)
转载请注明出处:[http://www.cnblogs.com/dennisit/p/3590664.html]