转载请声明出处:http://blog.csdn.net/zhongkelee/article/details/45334589
/*
1.建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。
*/
class ParaMatchException extends RuntimeException{
ParaMatchException(){
super();
}
ParaMatchException(String message){
super(message);
}
}
interface Graph{
public abstract double area();
}
class Circle implements Graph{
public static final double PI = 3.14;
private double r;
public Circle(double r){
if (r < 0)
throw new ParaMatchException("圆形半径不可以为负数");
this.r = r;
}
public double area(){
return PI*r*r;
}
}
class Rect implements Graph{
private double length;
private double width;
public Rect(double length, double width){
if (length<0 || width<0)
throw new ParaMatchException("矩形边长不可以为负数");
this.length = length;
this.width = width;
}
public double area(){
return length*width;
}
}
class ObjectTest1{
public static void main(String[] args){
Circle c = new Circle(3.4);
double result = c.area();
System.out.println("circle area is "+result);
Rect r = new Rect(3.4,-8.9);
double result1 = r.area();
System.out.println("rectangular area is "+result1);
}
}
/*
2.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某一个字符,
如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),
否则,返回-1。要搜索的字符数组和字符都以参数形式传递给该方法,
如果传入的数组为null,应抛出IllegalArgumentException异常。
在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,
例如,字符不存在,字符存在,传入的数组为null等。
*/
/*
分析:
数组为null时,不必在进行查找过程,所以应该继承RuntimeException。
class IllegalArgumentException extends RuntimeException{
IllegalArgumentException(){
super();
}
IllegalArgumentException(String message){
super(message);
}
}
*/
class ObjectTest2{
public static void main(String[] args){
char[] arr = new char[10];
for (int i = 0; i < 10; i++)
arr[i] = (char)(97+i);
for (int i = 0; i < 10; i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.print("y: ");
int locate = searchElem(arr, 'y');
System.out.println(locate);
}
public static int searchElem(char[] arr, char c){
if (arr == null)
throw new IllegalArgumentException("传入的字符数组为null");
for (int i = 0; i < arr.length; i++){
if (c == arr[i])
return i;
}
return -1;
}
}
/*
3.补足compare函数内的代码,不许添加其它函数。
*/
class Circle{
private double radius;
public Circle(double r){
radius = r;
}
public static double compare(Circle[] cir){
//程序代码,获取数组中的最大值
if (cir == null)
throw new IllegalArgumentException("输入的数组为null");
double max = cir[0].radius;//初始化为数组中的任意一个元素
//int max = 0;//初始化为数组中的任意一个角标
for (int i = 0; i < cir.length; i++){
if (cir[i].radius > max)
max = cir[i].radius;
}
return max;
}
}
class ObjectTest3{
public static void main(String[] args){
Circle cir[] = new Circle[3];
System.out.println(cir[0]);//null
cir[0] = new Circle(1.0);
cir[1] = new Circle(2.0);
cir[2] = new Circle(4.0);
System.out.println("最大的半径值是: "+Circle.compare(cir));
}
}
/*
4.描述Person
属性:
姓名和年龄。
行为:
1.说出姓名和年龄;
2.判断是否是同一个人(同姓名、同年龄视为同一个人)。
提示:
判断姓名相同的方法到API文档String类中查找。
*/
class Person{
private String name;
private int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public void speak(){
System.out.println("name: "+this.name+", age: "+this.age);
}
/**
既然要判断对象是否相同,直接使用Object类中的方法。
但是还要依据子类的特征来判断,直接覆盖Object类中的方法equals。
*/
public boolean equals(Object obj){
//判断姓名和年龄是否相同。这些都是Person的属性,所以必须向下转型。
if (!(obj instanceof Person))
throw new ClassCastException("类型错误");
Person p = (Person)obj;
return this.age==p.age && this.name.equals(p.name);
}
}
/*
5.补足代码
*/
class Circle{
private double radius;
public Circle(double r){
radius = r;
}
//请定义功能:比较两个圆是否一样大。
/*覆盖equals方法。*/
public boolean equals(Object obj){
if (!(obj instanceof Circle))
throw new ClassCastException(obj.getClass().getName()+"类型错误");
Circle c =(Circle)obj;
return this.radius == c.radius;
}
}
class ObjectTest5{
public static void main(String[] args){
Circle cir1 = new Circle(2.0);
Circle cir2 = new Circle(2.0);
boolean b = cir1.equals(cir2);
System.out.println("两个圆是否一样大:"+b);
}
}
public class OuterClass{
private double d1 = 1.0;
//insert code here
}
/*
6.把下列答案存放在指定code位置上,哪个答案是正确的。阐述原因。
*/
A.
class InnerOne{
public static double method(){return d1;}
}
不行,因为非静态内部类中,不可以定义静态方法。
B.
public class InnerOne{
static double method(){return d1;}
}
不行,因为非静态内部类中,不可以定义静态方法。
C.
private class InnerOne{
double method(){return d1;}
}
对!
D.
static class InnerOne{
protected double method(){return d1;}
}
不行,静态内部类只能访问外部类中的静态成员。
E.
abstract class InnerOne{
public abstract double method();
}
可以。
/*
7.写出下面代码的执行结果(需写出分析过程)
*/
class A{
void fun1(){
System.out.println(this.fun2());//this = new B();0x0099//A a = new B();a.fun2();多态:编译看左边,运行看右边。
}
int fun2(){
return 123;
}
}
class B extends A{
int fun2(){
return 456;
}
public static void main(String[] args){
A a;
B b = new B();//0x0099
b.fun1();//456
a = b;
a.fun1();//456
}
}
/*
8.写出下面代码的执行结果(需写出分析过程)
*/
import java.io.IOException;
public class ObjectTest8{
public static void main(String[] args){
try{
new ObjectTest8().methodA(5);
}catch(IOException e){
System.out.println("caught IOException");
}catch(Exception e){
System.out.println("caught Exception");
}finally{
System.out.println("no Exception");
}
}
void methodA(int i) throws IOException{
if (i%2 != 0)
throw new IOException("methodA IOException");
}
}
/*
9.写出程序结果
*/
interface A{}
class B implements A{
public String func(){
return "func";
}
}
class ObjectTest9{
public static void main(){
A a = new B();//多态,B对象已经向上提升了。
System.out.println(a.func());//编译失败,因为a所属的A接口中没有定义func方法。对于非静态方法,编译看左边,运行看右边。
}
}
/*
10.写出程序结果
*/
class Fu{
boolean show(char a){
System.out.println(a);
return true;
}
}
class ObjectTest10 extends Fu{
public static void main(String[] args){
int i = 0;
Fu f = new ObjectTest10();
ObjectTest10 obj = new ObjectTest10();
for (f.show('A'); f.show('B')&&(i<2); f.show('C')){
i++;
obj.show('D');
}
}
boolean show(char a){
System.out.println(a);
return false;
}
}
/*
11.写出程序结果
*/
interface A{}
class B implements A{
public String test(){
return "yes";
}
}
class ObjectTest11{
static A get(){
return new B();
}
public static void main(String[] args){
A a = get();//A a = new B();
System.out.println(a.test());//编译失败,A接口中没有定义test方法。
}
}
/*
12.写出程序结果
*/
class Super{
int i = 0;
public Super(String a){
System.out.println("A");
i = 1;
}
public Super(){
System.out.println("B");
i += 2;
}
}
class ObjectTest12 extends Super{
public ObjectTest12(String a){
//super();
System.out.println("C");
i += 5;
}
public static void main(String[] args){
int i = 4;//局部变量
Super d = new ObjectTest12("A");
System.out.println(d.i);
}
}
/*
13.补足代码
*/
interface Inter{
void show(int a, int b);
void func();
}
class ObjectTest13{
public static void main(String args){
//补足代码:调用两个函数,要求匿名内部类
Inter in = new Inter(){
public void show(int a, int b){}
public void func(){}
};//匿名内部类其实就是一个子类对象。这是一个多态。
in.show(3, 4);
in.func();
}
}
/*
14.写出程序结果
*/
class TD{
int y = 6;
class Inner{
static int y = 3;//编译会失败,因为非静态的内部类中不可以定义静态的成员。
void show(){
System.out.println(y);
}
}
}
class ObjectTest14{
public static void main(String[] args){
TD.Inner ti = new TD().new Inner();
ti.show();
}
}
/*
15.选择题,写出错误答案的原因。
*/
class Demo{
int show(int a, int b){return 0;}
}
//下面哪些函数可以存在于Demo的子类中,
A.public int show(int a, int b){return 0;}//可以的,覆盖。
B.private int show(int a, int b){return 0;}//不可以,权限不够。
C.private int show(int a, long b){return 0;}//可以,子类特有方法。
D.public short show(int a, int b){return 0;}//不可以,返回值不同导致的调用不确定性。
E.static int show(int a, int b){return 0;}//不可以,静态只能覆盖静态。
/*
16.写出程序结果
*/
class Fu{
int num = 4;
void show(){
System.out.println("showFu..."+num);
}
}
class Zi extends Fu{
int num = 5;
void show(){
System.out.println("showZi..."+num);
}
}
class ObjectTest15{
public static void main(String[] args){
Fu f = new Zi();
Zi z = new Zi();
System.out.println(f.num);//4
System.out.println(z.num);//5
f.show();//showzi
z.show();showzi
}
}
/*
17.补足代码
*/
interface A{
void show();
}
interface B{
void add(int a, int b);
}
class C implements A,B{
//程序代码
private int a,b;
public void add(int a, int b){
this.a = a;
this.b = b;
}
public void show(){
System.out.println(this.a+this.b);
}
}
class ObjectTest16{
public static void main(String[] args){
C c = new C();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和。
}
}
/*
18.写出程序结果
*/
class ObjectTest17{
public static void main(String[] args){
try{
showExce();
System.out.println("A");
}catch(Exception e){
System.out.println("B");
}finally{
System.out.println("C");
}
System.out.println("D");
}
public static void showExce() throws Exception{
throw new Exception();
}
}
/*
19.写出程序结果
*/
class Super{
int i = 0;
public Super(String s){
i = 1;
}
}
class ObjectTest18 extends Super{
public ObjectTest18(String s){
//super();//编译出错,父类没有空参的构造函数。
i = 2;
}
public static void main(String[] args){
ObjectTest18 d = new ObjectTest18("yes");
System.out.println(d.i);
}
}
/*
20.写出程序结果
*/
class Super{
public int get(){return 4;}
}
class ObjectTest19 extends Super{
public long get(){return 5;}//编译失败,覆盖失败,调用的不确定性。
public static void main(String[] args){
Super s = new ObjectTest19();
System.out.println(s.get());
}
}
/*
21.写出程序结果
*/
class ObjectTest20{
public static void func(){
try{
throw new Exception();
System.out.println("A");//该条语句无法被执行,废话!已经明显知道是异常,不应该在threw后面再加语句了!
}catch(Exception e){
System.out.println("B");
}
}
public static void main(String[] args){
try{
func();
}catch(Exception e){
System.out.println("C");
}
System.out.println("D");
}
}
/*
22.补足代码
*/
interface Test{
void func();
}
class ObjectTest21{
public static void main(String[] args){
//补足代码:(匿名内部类)调用show方法
new ObjectTest21().show(new Test(){
public void func(){}
});
}
void show(Test t){
t.func();
}
}
/*
23.写出程序结果
*/
class ObjectTest22{
public static String output="";
public static void foo(int i){
try{
if (i == 1)
throw new Exception();
output += "1";
}catch(Exception e){
output += "2";
return;
}finally{
output += "3";
}
output += "4";
}
public static void main(String[] args){
foo(0);
System.out.println(output);//134
foo(1);
System.out.println(output);//13423
}
}
/*
24.写出程序结果
*/
class ObjectTest23{
private static int j = 0;
private static boolean methodB(int k){
j += k;
return true;
}
public static void methodA(int i){
boolean b;
b = i < 10 | methodB(4);
b = i < 10 || methodB(8);
}
public static void main(String[] args){
methodA(0);
System.out.println(j);//4
}
}
如果弄懂了上面的这24道题,那么恭喜你,基本掌握了J2SE面向对象的思想~
下面我把J2SE前半部分的内容总结如下:
好了,后面我们就开始进入Java多线程技术的分享。
有任何问题请和我联系,共同进步:lichunchun4.0@gmail.com
转载请声明出处:http://blog.csdn.net/zhongkelee/article/details/45334589