习题
1
this用法:
表示当前对象引用,常用于形参或局部变量与类的成员变量同名的情形,使用this.成员名表示当前对象的成员表示当前对象调用当前类的构造方法
super用法:
子类的数据成员或成员方法与父类的数据成员或成员方法名字相同时,当要调用父类的同名方法或同名数据成员时则可用super来指明。即super.数据成员;super.成员方法表示调用父类构造方法
2
为对象分配内存空间,对成员变量进行默认的初始化绑定构造方法,将new中的参数传递给构造方法的形式参数。调用this或super语句(二者必居其一,不能同时存在)执行当前构造方法的方法体中的程序代码。进行实例变量的显式初始化操作
3
类的域变量在对象实例化的时候系统对其进行默认的初始化,之后可以通过构造方法在对象实例化的过程中改变其值 局部变量在声明的时候并对其赋初值,仅仅是在栈内存上为其分配空间,需要在访问该变量之前对其赋值
4
package my
;
abstract class Room{
public int WindowsNum
;
public String WallColor
;
public double Area
;
public void EchoWindowNum(int WindowNum
){
this.WindowsNum
= WindowNum
;
System
.out
.println("This is a room with "+WindowNum
+" windows.");
}
public void EchoWallColor(String WallColor
){
this.WallColor
= WallColor
;
System
.out
.println("the wall is "+WallColor
+".");
}
public void EchoArea(double Area
){
this.Area
= Area
;
System
.out
.println("and the area is "+Area
+".");
}
}
class aRoom extends Room{
public void Praise(){
System
.out
.println("Beautiful Room!");
}
}
public class AbstractTest {
public static void main(String
[] args
){
aRoom TestRoom
= new aRoom();
TestRoom
.EchoWindowNum(3);
TestRoom
.EchoWallColor("white");
TestRoom
.EchoArea(100.0);
TestRoom
.Praise();
}
}
运行结果:
5
接口: 接口相当于一种标准,类实现接口时需要实现接口上所有的方法。
package my
;
interface RullOfRoom{
void wall();
void door();
}
class CreateRoom implements RullOfRoom{
public void wall(){
System
.out
.println("There are 4 walls");
}
public void door(){
System
.out
.println("There is 1 door");
}
}
public class InterfaceTest {
public static void main(String
[] args
){
CreateRoom room
= new CreateRoom();
room
.wall();
room
.door();
}
}
如果接口的某一个方法未实现:
6
抽象类与接口的异同: 相同点:两者都可以具有抽象方法,但都不能实例化,都可以有自己的声明,并能够引用子类对象或者实现类对象 不同点: 1.新类可以组合多个接口,但只能继承单一的抽象类 2.接口不能包含除了 static属性之外的其他属性,但是抽象类可以包含属性,非抽象方法可以引用这些属性 3.接口不需要在子类中实现默认方法,默认方法可以引用其他接口的方法,抽象类可以有具体的方法,具体方法可以调用抽象方法,其子类必须实现抽象方法 4.接口没有构造器,抽象类可以有构造器 5.接口默认为 public,抽象类可以使 protected或者友元
7
引用的方法: 1.equals方法,比较本引用和参数指明的某个引用是否相同,即是否指向同一个对象,返回 true或者 false 2.使用 == 比较,如果两边是对象引用则比较他们的引用是否相同 3.instanceof,二元运算符,判断某个对象是否是指定类的实例
8
内部类作用: 1.实现隐藏,一般的非内部类,是不允许有 private 与protected权限的,但内部类可以 2.内部类可以直接访问外部类的所有成员 匿名内部使用情况: 1.对一个对象只需要进行一次方法调用 2.将匿名对象作为参数传递给一个函数调用
9
B
10
子类和父类具有同名变量时,访问该变量时为子类的变量而不是父类的变量
11
1,2,3,4,5
补充题
1
2
package my
;
class Sender {
receiver R
;
Sender(receiver R
) {
this.R
= R
;
}
void SendMessage() {
System
.out
.println("Message sended");
R
.act();
}
void Success() {
System
.out
.println("information receiver");
System
.out
.println("Act successfully");
}
void fail() {
System
.out
.println("information receiver");
System
.out
.println("Act failed");
}
}
class receiver {
Sender S
;
Boolean CanAct
;
void ReceiveMessage(Sender S
) {
this.S
= S
;
}
void act() {
System
.out
.println("receive message");
if (CanAct
== Boolean
.TRUE
) {
System
.out
.println("acted");
S
.Success();
} else {
System
.out
.println("can't act");
S
.fail();
}
}
}
public class MessageTest {
public static void main(String
[] args
) {
receiver receiver
= new receiver();
Sender sender
= new Sender(receiver
);
receiver
.ReceiveMessage(sender
);
sender
.SendMessage();
}
}
运行结果:
3
1.组合是指新类由现有类的对象合并而成,现有类作为新类的属性 2.继承是通过扩展已有类来获得新功能,实现代码重用 A is B 用继承,A has B用组合。
4
package my
;
abstract class Person{
abstract void FavoriteColor();
}
class Boys extends Person{
public void FavoriteColor(){
System
.out
.println("FavoriteColor is cool color");
}
public void GoodAt(){
System
.out
.println("Good at exercise");
}
}
class Girls extends Person{
public void FavoriteColor(){
System
.out
.println("FavoriteColor is warm color");
}
public void GoodAt(){
System
.out
.println("Good at art");
}
}
public class PolymorphismTest {
public static void main(String
[] args
){
show(new Boys());
show(new Girls());
Person a
= new Boys();
a
.FavoriteColor();
Boys b
= (Boys
)a
;
b
.GoodAt();
}
public static void show(Person a
){
a
.FavoriteColor();
if(a
instanceof Boys){
((Boys
) a
).GoodAt();
}
else if (a
instanceof Girls){
((Girls
) a
).GoodAt();
}
}
}
6
instance of运用场景: 用于判断某个对象是否为某个类的实例,如果对象是子类的实例则返回 true,如果对象是父类的实例,返回 false, 如果对象和类没有任何关系,则编译错误。