<pre name="code" class="java">class Factory
{
static Factory factory = new Factory();
private Factory(){}
int num =0;
public static Factory getInstance()
{
return factory;
}
public void jiaGong()
{
System.out.println("加工的是第"+(++num)+"个零件儿");
}
}
class Demo2
{
public static void main(String[] args)
{
Factory f1 = Factory.getInstance();
f1.jiaGong();
f1.jiaGong();
f1.jiaGong();
f1.jiaGong();
f1.jiaGong();
Factory f2 = Factory.getInstance();
f2.jiaGong();
f2.jiaGong();
f2.jiaGong();
f2.jiaGong();
f2.jiaGong();
}
}
/*设计模式:解决某一类问题最有效的方式单例设计模式:解决的问题是,一个类在内存中只有一个实例1:构造方法不能被外界使用2:只能是该类提供自身类型的对象3:能让外界获取到这个对象*///饿汉式class Single{static Single single = new Single();private Single(){}public static Single getInstance(){return single;}}//懒汉式class Single2{static Single2 single = null;private Single2(){}public static Single2 getInstance(){if(single==null)single = new Single2();return single;}}class Demo1{public static void main(String[] args) {Single s = Single.getInstance();Single s2 = Single.getInstance();System.out.println(s==s2);}}
class Factory
{
static Factory factory = new Factory();
private Factory(){}
int num =0;
public static Factory getInstance()
{
return factory;
}
public void jiaGong()
{
System.out.println("加工的是第"+(++num)+"个零件儿");
}
}
class Demo2
{
public static void main(String[] args)
{
Factory f1 = Factory.getInstance();
f1.jiaGong();
f1.jiaGong();
f1.jiaGong();
f1.jiaGong();
f1.jiaGong();
Factory f2 = Factory.getInstance();
f2.jiaGong();
f2.jiaGong();
f2.jiaGong();
f2.jiaGong();
f2.jiaGong();
}
}
class SuperMan
{
private String name;
static SuperMan superMan = new SuperMan("克拉克");
private SuperMan(){}
private SuperMan(String name)
{
this.name = name;
}
public static SuperMan getInstance()
{
return superMan;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
class Demo3
{
public static void main(String[] args)
{
SuperMan superMan = SuperMan.getInstance();
System.out.println(superMan.getName());
}
}
/*继承:1:提高了代码的重复利用,2:使类和类之间产生了关系
被继承的类是父类,继承的类称为子类
java中是单继承的:
·1:单继承:一个类只能有一个父类
2:多继承:一个类可以有多个父类
---单继承
class Fu
{
}
class Zi extends Fu
{
}
---多继承:会出现调用的不确定
class Fu1
{
public void show()
{
}
}
class Fu2
{
public void show()
{
}
}
class Zi extends Fu1,Fu2
{
}
Zi z = new Zi();
z.show();
支持多层继承:
class A
{
}
class B extends A
{
}
class C extends B
{
}
什么时候用继承?
不要为了重复利用代码而继承
事物之间存在所属关系时使用继承
比如:学生属于人的一种
猫属于动物的一种
*/
class Demo4
{
public static void main(String[] args)
{
}
}
class Animal
{
String name;
int age;
public void eat()
{
}
}
class Student extends Animal
{
public void study()
{
}
}
class Worker extends Person
{
public void work()
{
}
}
/*
继承中成员的特点:
成员变量:
成员函数
构造函数
this是一个引用
super用于在子类中访问父类中的成员
父类中私有的成员,子类也会继承过来,只是无权访问,通过父类提供的;
*/
class Fu
{
private int num = 5;
public int getNum()
{
return num;
}
}
class Zi extends Fu
{
int num = 6;
public void show()
{
//System.out.println(this.num);
//System.out.println(super.num);
System.out.println(getNum());
}
}
class Demo6
{
public static void main(String[] args)
{
Zi z = new Zi();
//System.out.println(z.num);
//System.out.println(z.num2);
z.show();
}
}
//成员函数
//子父类中出现了一模一样的方法(包括返回值类型),这种现象叫重写,覆盖
//子类一旦重写了父类中的方法,使用子类对象调用重写的方法执行的是子类中的
class Fu
{
public void show()
{
System.out.println("fu");
}
}
class Zi extends Fu
{
public void show()
{
System.out.println("zi");
}
}
class Demo7
{
public static void main(String[] args)
{
Zi z= new Zi();
z.show();
}
}
/*
class Phone
{
public void call()
{}
public void tel()
{
System.out.println("显示号码");
}
}
class NewPhone extends Phone
{
//沿袭了父类功能,实现不同了
public void tel()
{
super.tel();
System.out.println("显示名字");
}
}
*/
/*
class ZhangSan
{
public void eat()
{
System.out.println("细嚼慢咽");
}
}
class ZhangXiaoSan extends ZhangSan
{
public void eat()
{
System.out.println("狼吞虎咽");
}
}
*/
/*
子父类中构造函数的特点:
在创建子类对象时,总是先调用父类的构造方法,再调用子类的构造方法,原因是系统默认在子类的构造方法的第一行加入了一条语句
super(),这条语句默认是调用父类中无参的构造方法
为什么先去调用的父类的构造方法:先去执行父类的构造方法,可以使用父类构造方法中的初始化功能
如果父类中没有无参的构造方法,那么子类必须在构造方法的第一行手动加上super调用父类中的某一个构造方法
*/
class Fu
{
String name;
int age;
//Fu(){}
Fu(String name,int age)
{
this.name = name;
this.age = age;
}
}
class Zi extends Fu
{
Zi()
{
super("lisi",5);
System.out.println("zi");
}
Zi(String name,int age)
{
super(name,age);
//this.name = name;
//this.age = age;
}
}
class Demo8
{
public static void main(String[] args)
{
Zi z = new Zi();
}
}
class Fu
{
int age;
Fu(){}
Fu(int age)
{
this.age = age;
}
}
class Zi
{
Zi()
{
//super();
System.out.println("Hello World!");
}
Zi(int age)
{
this(); //this和super()都必须写在第一行,所以用了this就不能使用super()了
//super(age);
}
}
class Demo9
{
public static void main(String[] args)
{
Zi z = new Zi(67);
}
}
/*
final:修饰符,可以修饰 类,变量,方法
一个类被修饰为final的,那么这个类不能被继承了
*/
class Test
{
public static final double PI = 3.1415;//符号常量了
double radius =12.3;
public void show()//这个方法不能被重写
{
}
public double area()
{
return PI* radius*radius;
}
}
class Demo10
{
public static void main(String[] args)
{
final int a=889;
// a = 56;
Test t = new Test();
t.show();
show(1,2);
}
public static int show(final int a,final int b)
{
a = 34;
b = 67;
return a+b;
}
}
class Single
{
static final Single single = new Single();//引用single不能再指向其他的对象了
private Single(){}
public static Single getInstance()
{
return single;
}
}
/*
abstract:抽象的
*/
class Demo11
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
//如果一个类中含有抽象方法,那么这个类必须是抽象的
//抽象类:同样是类,同样是在描述事物,只不过出现了没有足够的信息描述事物方法
abstract class 犬科
{
public abstract void 吼叫();//没有足够的信息描述事物的行为
}
class 狗
{
public void 吼叫()
{
System.out.println("汪汪");
}
}
class 狼
{
public void 吼叫()
{
System.out.println("嗷嗷");
}
}
//抽象类的特点:
//1:抽象类不能创建对象
//2:抽象类的子类没有重写抽象类中的抽象方法,那么这个子类也是抽象类
/*
1:抽象类一定是父类吗?
一定是父类
2:抽象类有构造方法吗?
有构造方法,用来给子类进行初始化
抽象类和普通类有什么区别?
抽象类也是类,和普通类的相同点:都是类,都是用来描述事物的
不同点:1:抽象类不能创建对象,普通类可以创建对象
2:抽象类可以含有抽象方法,普通类不可以
3:抽象类和哪些修饰符不能同时使用?
1:final:抽象类需要被继承,被final修饰的类不能被继承
2:static:抽象方法不能通过类名调用,静态方法可以通过类名调用
3:private:抽象方法必须被子类可以覆盖,私有的方法不能被子类覆盖
4:抽象类一定含有抽象方法吗?
不一定,当不希望一个类被创建对象时,可以把这个类修饰为abstract
*/
//计算圆形和矩形的面积
abstract class Shape
{
public abstract double getArea();
}
abstract class Circle extends Shape
{
public static final double PI = 3.1415;
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
//没有覆盖父类中的抽象方法,那么该子类中也含有抽象方法,那么这个子类也是抽象类
//public abstract double getArea();
}
class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle(double length,double width)
{
this.length = length;
this.width = width;
}
public double getArea()
{
return this.length*this.width;
}
}
class Demo12
{
public static void main(String[] args)
{
//Rectangle r = new Rectangle(10,10);
//System.out.println(r.getArea());
Circle c = new Circle();
//Shape s = new Shape();
//s.getArea();
}
}
/*
需求:公司中程序员有姓名,工号,薪水,工作内容。
项目经理除了有姓名,工号,薪水,还有奖金,工作内容。
对给出需求进行数据建模。
*/
abstract class Employee
{
private String name;
private String id;
private double money;
Employee(){}
Employee(String name,String id,double money)
{
this.name = name;
this.id = id;
this.money = money;
}
public abstract void work();
}
class Programmer extends Employee
{
Programmer(){}
Programmer(String name,String id,double money){
super(name,id,money);
}
public void work()
{
System.out.println("敲代码");
}
}
class Manager extends Employee
{
private double bonus;
Manager(){}
Manager(String name,String id,double money,double bonus)
{
super(name,id,money);
this.bonus = bonus;
}
public void work()
{
System.out.println("管理程序员");
}
}
class Demo13
{
public static void main(String[] args)
{
Programmer pro = new Programmer("小黑","110",10000);
pro.work();
Manager manager = new Manager("小白","119",15000,5000);
manager.work();
}
}
/*
接口:定义形式
interface
{
全局常量(public static final)
抽象方法(public abstract)
}
接口可以解决Java中单继承的问题
接口可以多实现,实现多个接口不会出现调用的不确定,因为接口中的方法都是抽象的,都没有实现
在创建子类对象调用接口中的方法时,很明确是在调用子类中覆盖的方法
*/
// 把接口看成特殊的类
interface inter
{
public static final int NUM = 110;
public abstract void show();
}
interface inter2
{
public abstract void show();
}
class Test implements inter,inter2
{
public void show()
{
System.out.println("show");
}
}
class Demo14
{
public static void main(String[] args)
{
Test t = new Test();
//t.show();
System.out.println(Test.NUM);
System.out.println(t.NUM);
System.out.println(inter.NUM);
}
}
/*
一个类在继承的同时还可以去实现接口
类和类之间是继承
类和接口之间是实现
父类提供该继承体系的最基本的功能
实现可以得到继承以外的额外功能
*/
class Person
{
}
interface inter
{
public static final int NUM = 110;
public abstract void show();
}
class Student extends Person implements inter
{
}
/*
接口和接口之间可以多继承
interface a
{}
interface b extends a
{}
interface c extends a,b
{}
*/
import java.util.*;
/*
接口的好处:
1:接口可以提高程序的扩展性
2:接口是对外暴露的一些规则
3:接口具备强制性,降低了类之间的耦合性
*/
interface USB
{
}
/*
抽象类和接口的区别:
犬科动物:警犬,导盲犬,搜爆犬
abstract class 犬科
{
public abstract void 训练();
}
class 警犬 extends 犬科
{
public abstract void 训练(){
}
public void 破案 ()
{}
}
class 导盲犬 extends 犬科
{
public abstract void 训练(){
}
public void 导盲 ()
{}
}
interface soubao
{
}
class 搜爆犬 extends 犬科 implements soubao
{
public abstract void 训练(){
}
public void 搜爆 ()
{}
}
class 搜爆猫 implements soubao
{
public void 搜爆 ()
{}
}
class 搜爆猪 implements soubao
{
public void 搜爆 ()
{}
}
*/
class Demo16
{
public static void main(String[] args)
{
Properties pro = System.getProperties();
System.out.println(pro);
}
}
/*
多态:多种形态
class Animal
{
}
class 猫 extends Animal
{
}
猫:可以看成猫 猫 mao = new 猫();//常态
可以看成动物 Animal mao = new 猫();//多态
多态:父类类型的引用指向了子类类型的对象
多态的前提:存在继承或实现
多态的弊端:只能使用父类中定义的功能,子类特有的功能不能调用了
*/
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("猫吃鱼");
}
public void catchMouse()
{
System.out.println("猫抓耗子");
}
}
class Demo17
{
public static void main(String[] args)
{
Cat cat = new Cat();
cat.eat();
cat.catchMouse();
Animal mao = new Cat();
mao.eat();
//mao.catchMouse();//把子类类型的对象看成父类类型,就只能使用父类中定义的功能了
}
}
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("猫吃鱼");
}
public void catchMouse()
{
System.out.println("猫抓耗子");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("狗吃骨头");
}
public void kanJia()
{
System.out.println("狗看家");
}
}
class Demo18
{
public static void main(String[] args)
{
Cat cat = new Cat();
//cat.eat();
chi(cat);
Dog dog1 = new Dog();
//dog1.eat();
chi(dog1);
}
public static void chi(Animal animal) //Animal animal = new Cat(); Animal animal = new Dog();
{
animal.eat();
animal.kanJia();
}
/*
public static void chi(Cat cat)
{
cat.eat();
}
public static void chi(Dog dog)
{
dog.eat();
}
public static void chi(Pig pig)
{
pig.eat();
}
*/
}
//在多态中使用子类特有的功能
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("猫吃鱼");
}
public void catchMouse()
{
System.out.println("猫抓耗子");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("狗吃骨头");
}
public void kanJia()
{
System.out.println("狗看家");
}
}
class Demo19
{
public static void main(String[] args)
{
Animal animal = new Cat();// 向上转型--自动转换
//animal.eat();
//使用子类特有的功能
Cat cat = (Cat)animal;//向下转型--强制转
//cat.catchMouse();
chi(cat);
}
public static void chi(Animal animal)//Animal animal = new Cat(); Animal animal = new Dog();
{
animal.eat();
if(animal instanceof Cat)
{
Cat cat = (Cat)animal;
cat.catchMouse();
}
if(animal instanceof Dog)
{
Dog dog = (Dog)animal;
dog.kanJia();
}
}
}
/*
多态中成员的特点:
成员变量:在多态中编译时期能够使用哪些变量看父类,执行结果也看父类
成员函数:编译时期能够使用哪些方法看父类,执行结果看子类,如果子类重写了父类中的方法,看子类,否则看父类
静态成员函数:编译执行都看父类
*/
class Fu
{
int num1 = 5;
public void show()
{
System.out.println("fu");
}
public static void ff()
{
System.out.println("fu static ");
}
}
class Zi extends Fu
{
int num1 = 6;
public void show()
{
System.out.println("zi");
}
public void fun()
{
System.out.println("fun");
}
public static void ff()//静态只能覆盖静态
{
System.out.println("zi static ");
}
}
class Demo20
{
public static void main(String[] args)
{
Fu f = new Zi();
System.out.println(f.num1);
//System.out.println(f.num2);
f.show();
f.ff();
//f.fun();
}
}
/*
接口和多态
*/
interface inter
{
public void show();
}
class Test1 implements inter
{
public void show()
{
System.out.println("test1");
}
}
class Test2 implements inter
{
public void show()
{
System.out.println("test2");
}
}
class Demo21
{
public static void main(String[] args)
{
Test1 t = new Test1();
//t.show();
fun(t);
Test2 t2 = new Test2();
//t2.show();
fun(t2);
}
public static void fun(inter in)//inter in = new Test1(); inter in = new Test2();
{
//return in;
in.show();
}
/*
public static void fun(Test1 t)
{
t.show();
}
public static void fun(Test2 t)
{
t.show();
}
*/
}
/*
狗生活在陆地上(是一种陆生动物),既是哺乳类的也是肉食性的。
狗通常的时候和人打招呼会通过“摇摇尾巴”,在被抚摸感到舒服的时候,
会“旺旺叫”,而在受到惊吓情绪烦躁时,会发出“呜呜”声;
猫也生活在陆地上(是一种陆生动物),既是哺乳类的也是肉食性的。
猫通常的时候和人打招呼会发出“喵~”的声音,在被抚摸情绪很好时,
会发出“咕噜咕噜”声,而在受到惊吓时,会发出“嘶嘶”声;
青蛙是一种两栖动物(既是水生动物也是陆生动物),
既不是哺乳类的也不是肉食性的,属于卵生。当青蛙情绪好的时候,
会在岸边“呱呱呱”的唱歌,而在受到惊吓时,会“扑通一声跳入水中”;
*/
abstract class Animal
{
public static final int HAPPY = 1;
public static final int UNHAPPY = 2;
protected boolean mammal;
protected boolean carnivorous;
private int mood;
public void setMood(int mood)
{
this.mood = mood;
}
public int getMood()
{
return this.mood;
}
public boolean isMammal()
{
return this.mammal;
}
public boolean isCarnivorous()
{
return this.carnivorous;
}
public abstract String sayHello();
public abstract String sayHello(int mood);
}
interface Land
{
public abstract int getNumberOfLegs();
}
interface Water
{
public abstract boolean getGillFlags();
public abstract boolean getLayEggsFlags();
}
class Frog extends Animal implements Land,Water
{
private int numberOfLegs = 4;
private boolean gillFlag = false;
private boolean layEggsFlag = true;
public Frog()
{
this.mammal = false;
this.carnivorous = false;
}
public String sayHello()
{
return "呱呱";
}
public String sayHello(int mood)
{
this.setMood(mood);
int m = this.getMood();
switch(m)
{
case HAPPY:
return "呱呱呱";//break;
case UNHAPPY:
return "扑通一声跳入水中";
default:
return "呱呱";
}
}
public boolean getGillFlags()
{
return this.gillFlag;
}
public boolean getLayEggsFlags()
{
return this.layEggsFlag;
}
public int getNumberOfLegs()
{
return this.numberOfLegs;
}
}
class Cat extends Animal implements Land
{
private int numberOfLegs = 4;
public Cat()
{
this.mammal = true;
this.carnivorous = true;
}
public String sayHello()
{
return "喵~";
}
public String sayHello(int mood)
{
this.setMood(mood);
int m = this.getMood();
switch(m)
{
case HAPPY:
return "咕噜咕噜";//break;
case UNHAPPY:
return "嘶嘶";
default:
return "喵~";
}
}
public int getNumberOfLegs()
{
return this.numberOfLegs;
}
}
class Demo22
{
public static void main(String[] args)
{
Cat cat = new Cat();
if(cat.isMammal())
System.out.println("猫是哺乳动物");
else
System.out.println("猫不是哺乳动物");
if(cat.isCarnivorous())
System.out.println("猫是肉食动物");
else
System.out.println("猫不是肉食动物");
System.out.println("猫通常情况下和人打招呼的方式是:"+cat.sayHello());
System.out.println("猫情绪好时和人打招呼的方式是:"+cat.sayHello(Animal.HAPPY));
System.out.println("猫情绪不好时和人打招呼的方式是:"+cat.sayHello(Animal.UNHAPPY));
System.out.println("猫是陆生动物,猫有"+cat.getNumberOfLegs()+"条腿儿");
}
}
注:按Java规范书写程序代码,如果你认为程序有错误,请指出,并说明程序错误原因。
1.
写出程序结果
class Demo
{
public static void func()throws Exception
{
try
{
throw new Exception();
}
finally
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
如果func放上声明了该异常。结果是?B C D
====================================================================
2.
写出程序结果
class Test
{
Test()
{ //super();
System.out.println("Test");
}
}
class Demo extends Test
{
Demo()
{
//super();
System.out.println("Demo");
}
public static void main(String[] args)
{
new Demo();//Test Demo
new Test();//Test
}
}
====================================================================
3.
写出程序结果
interface A{}
class B implements A
{
public String func()
{
return "func";
}
}
class Demo
{
public static void main(String[] args)
{
A a=new B();
System.out.println(a.func());
}
}
//编译出错
====================================================================
4.
写出程序结果
class Fu
{
boolean show(char a)
{
System.out.println(a);
return true;
}
}
class Demo extends Fu
{
public static void main(String[] args)
{
int i=0;
Fu f=new Demo();
Demo d=new Demo();
for(f.show('A'); f.show('B')&&(i<2);f.show('C'))
{
i++;
d.show('D');
}
}
boolean show(char a)
{
System.out.println(a);
return false;
}
}
A B
====================================================================
5.
写出程序结果
interface A{}
class B implements A
{
public String test()
{
return "yes";
}
}
class Demo
{
static A get()
{
return new B();
}
public static void main(String[] args)
{
A a=get();//A a = new B();
System.out.println(a.test());
}
}
//编译出错
====================================================================
6.
写出程序结果:
class Super
{
int i=0;
public Super(String a)
{
System.out.println("A");
i=1;
}
public Super()
{
System.out.println("B");
i+=2;
}
}
class Demo extends Super
{
public Demo(String a)
{
//super();
System.out.println("C");
i=5;
}
public static void main(String[] args)
{
int i=4;
Super d=new Demo("A");//B C
System.out.println(d.i);//5
}
}
====================================================================
7.
interface Inter
{
void show(int a,int b);
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;调用两个函数,要求用匿名内部类
Inter inter = new Inter(){
public void show(int a,int b)
{
}
public void func()
{}
};
inter.show(2,3);
inter.func();
}
}
====================================================================
8.
写出程序结果
class TD
{
int y=6;
class Inner
{
static int y=3;
void show()
{
System.out.println(y);
}
}
}
class TC
{
public static void main(String[] args)
{
TD.Inner ti=new TD().new Inner();
ti.show();
}
}
//编译出错,内部类含有静态成员,内部类必须是静态的
====================================================================
9.
选择题,写出错误答案错误的原因,用单行注释的方式。
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;}//不可以,静态只能覆盖静态
====================================================================
10.
写出this关键字的含义,final有哪些特点?
this:代表本类对象,哪个对象调用this所在函数,this就代表哪个对象。
final:
1,修饰类,变量(成员变量,静态变量,局部变量),函数。
2,修饰的类不可以被继承。
3,修饰的函数不可以被覆盖。
4,修饰的变量是一个常量,只能赋值一次。
====================================================================
11.
写出程序结果:
class Fu
{
int num=4;
void show()
{
System.out.println("showFu");
}
}
class Zi extends Fu
{
int num=5;
void show()
{
System.out.println("showZi");
}
}
class T
{
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
}
}
====================================================================
12.
interface A
{
void show();
}
interface B
{
void add(int a,int b);
}
class C implements A,B
{
//程序代码
private int a,b;
private int sum;
public void add(int a,int b)
{
this.a =a;
this.b = b;
this.sum = a+b;
}
public void show()
{
System.out.println(sum);
}
}
class D
{
public static void main(String[] args)
{
C c=new C();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和。
}
}
====================================================================
13.
写出程序结果
class Demo
{
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();
}
}
B C D
====================================================================
14.
写出程序结果
class Super
{
int i=0;
public Super(String s)
{
i=1;
}
}
class Demo extends Super
{
public Demo(String s)
{
//super();
i=2;
}
public static void main(String[] args)
{
Demo d=new Demo("yes");
System.out.println(d.i);
}
}
//编译出错,父类中没有空参的构造方法
====================================================================
15.
写出程序结果
class Super
{
public int get(){return 4;}
}
class Demo15 extends Super
{
public long get(){return 5;}
public static void main(String[] args)
{
Super s=new Demo15();
System.out.println(s.get());
}
}
编译出错,因为 public long get(){return 5;}不是重写也不是重载
====================================================================
16.
写出程序结果:
class Demo
{
public static void func()
{
try
{
throw new Exception();
System.out.println("A");//这句话永远执行不到
}
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");
}
}
编译出错
====================================================================
17.
class Demo
{
public void func()
{
//位置1;
new Inner();
}
class Inner{}
public static void main(String[] args)
{
Demo d=new Demo();
// 位置2
new Inner();
}
}
A.在位置1写 new Inner(),可不可以?不可以写出原因;//可以
B.在位置2写 new Inner(),可不可以?不可以写出原因;//不可以,因为main是静态的,只能使用静态的
C.在位置2写 new d.Inner(),可不可以?不可以写出原因;// 不可以,new new Demo()
D.在位置2写 new Demo.Inner(),可不可以?不可以写出原因;//不可以,因为内部类不是静态的
====================================================================
18.
写出程序结果
class Exc0 extends Exception{}
class Exc1 extends Exc0{}
class Demo
{
public static void main(String[] args)
{
try
{
throw new Exc1();
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(Exc0 e)
{
System.out.println("Exc0");
}
}
}
编译出错,父类异常写在了子类异常的上边
====================================================================
19.
interface Test
{
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;(匿名内部类)
new Demo().show(new Test(){
public void func()
{ }
});
}
void show(Test t)
{
t.func();
}
}
====================================================================
20.
写出程序结果
class Test
{
public static String output="";
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+="1";
}
catch(Exception e)
{
output+="2"; //1342
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
}
}
====================================================================
21.
建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。
====================================================================
22.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private static double pi=3.14;
private double radius;
public Circle(double r)
{
radius=r;
}
public static double compare(Circle[] cir)
{
//程序代码//其实就是在求数组中的最大值。
int max =0;
for(int i=1;i<cir.length;i++)
{
if(cir[i].radius>cir[max].radius)
max = i;
}
return cir[max].radius;
}
}
class TC
{
public static void main(String[] args)
{
Circle cir[]=new Circle[3];//创建了一个类类型数组。
cir[0]=new Circle(1.0);
cir[1]=new Circle(2.0);
cir[2]=new Circle(4.0);
System.out.println("最大的半径值是:"+Circle.compare(cir));
}
}
====================================================================
23.
写出程序结果
public class Demo
{
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); //methodB (8)没执行
}
public static void main (String args[] )
{
methodA (0);
System.out.println(j);//4
}
}
====================================================================
24.
假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:
姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个
奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方
法进行属性访问。
====================================================================
25.
在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,
如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),
否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,
如果传入的数组为null,应抛出IllegalArgumentException异常。
在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,
例如,字符不存在,字符存在,传入的数组为null等。
getIndex(null,'a');
int getIndex(char[] arr,char key)
{
int index = -1;
if(arr ==null)
throw new IllegalArgumentException();
for(int i=0;i<arr.length;i++)
{
if(arr[i]==key)
{
index = i;
break;
}
}
return index;
}
====================================================================
26.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private double radius;
public Circle(double r)
{
radius=r;
}
public Circle compare(Circle cir)
{
//程序代码
return this.radius>cir.radius?this:cir;
}
}
class TC
{
public static void main(String[] args)
{
Circle cir1=new Circle(1.0);
Circle cir2=new Circle(2.0);
Circle cir;
cir=cir1.compare(cir2);
if(cir1==cir)
System.out.println("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");
}
}