Java学习笔记之内部类和异常类、匿名类

以下是博主学java的时候记的一些笔记,分享给大家,如果有错误或者以为的话,可以在下方留言

(1)外部类的成员变量在内部类中依然有效,内部类的方法也可以调用外部类的方法;

(2)在内部类的类体中不能声明类变量和类方法,在外部类的类体中可以用内部类声明对象作为外部类的成员。

例:

<span style="font-size:18px;"><span style="color:#000000;">package com.wanggang;

 

class RedFarm{

String name;

RedCow cow;

RedFarm(){

}

RedFarm(String s){

cow=new RedCow(110,120,130);

name=s;

}

public void show_cow(){

cow.speak();

//System.out.println("该牛的名字是:"+cow_name);

}

//public void hhh(){

//System.out.println("lalala");

//}

class RedCow{

String cow_name="红牛";

int wei,hei,pri;

RedCow(int wei,int hei,int pri){

this.wei=wei;

this.hei=hei;

this.pri=pri;

}

void speak(){

System.out.println("我是:"+cow_name+" 体重:"+wei+" 身高:"+" 价格:"+pri);

System.out.println("农场的名字是:"+name);

//RedFarm.hhh();

}

}

}

class A{

//RedCow ed=new RedCow(123,125,412);

 

}

public class hongniu {

public static void main(String []args){

RedFarm red=new RedFarm("红牛农场");

red.show_cow();

red.cow.speak();

}

}
</span>
 </span>

答案:

<span style="font-size:18px;">我是:红牛 体重:110 身高: 价格:130

农场的名字是:红牛农场

我是:红牛 体重:110 身高: 价格:130

农场的名字是:红牛农场

 </span>

static 内部类不能操作外部类的实例成员变量。

把上述的内部类ReaCow更改成static 内部类,那么就需要在main方法里面增加一下代码:RedCowFarm.RedCow redCow=new RedCowFarm.RedCow(180,119,6000);

red.speak();

 

匿名类

 

和类和接口有关的匿名类

假设People是一个类(接口),那么下面就是用People类(接口)的子类来创建对象的过程:

new People(){
      (接口的)匿名类的类体

};

个人理解:匿名类其实就是一个子类,匿名类可以继承父类的方法,也可以重写父类的方法,匿名类可以访问外部类的成员变量和方法,匿名类的类体中不可以声明static成员变量和方法.如果是一个接口要用用匿名类创建对象的话,匿名类的类体中一定要重写接口中的所有方法。匿名类其实没有名字,就是只是为了用它去创建一个对象,然后可以调用匿名类中类体的方法。

例:

<span style="font-size:18px;">package com.niminglei;

abstract class Speak{

public abstract void speakHello();

}

class Student{

void f(Speak sp){

sp.speakHello();

//System.out.println("a="+sp.a);

}

}

public class niminglei {

public static void main(String []args){

Speak speak=new Speak(){

int a=20;

public void speakHello(){

System.out.println("大家好,祝大家工作顺利");

System.out.println("a="+a);

}

};

speak.speakHello();

Student st=new Student();

st.f(new Speak(){

int a=10;

public void speakHello(){

System.out.println("I am a student,how are you?");

System.out.println("a="+a);

}

});

}

}</span>

答案:

<span style="font-size:18px;"><span style="color:#000000;">大家好,祝大家工作顺利

a=20

I am a student,how are you?

a=10</span></span>


异常类和finally子语句

 

个人理解:把异常的操作放在try中,try中的某个方法出现异常时,try部分立即结束操作,执行catch部分。

finally的作用就是,无论最后执行的是try还是catch,最后都会执行finally中的语句。

 

<span style="font-size:18px;">package com.lianshouyichang;

 

class Inter extends Exception{

final String message="超重";

public String set_message(){

return message;

}

}

class Coat{

int real;

int max;

public void set_max(int max){

this.max=max;

}

public void loat(int data) throws Inter{

real+=data;

if(real>max){

throw new Inter();

}

System.out.println("货船已经装了:"+real+",还未装满");

}

}

public class shahaizi{

public static void main(String []args){

Coat coat=new Coat();

coat.set_max(1100);

try{

coat.loat(1000);

coat.loat(90);

coat.loat(20);

}

catch(Inter e){

System.out.println(e.set_message());

}

finally{

System.out.println("出发!!!");

}

}

}

 

 </span>

答案:

<span style="font-size:18px;">货船已经装了:1000,还未装满

货船已经装了:1090,还未装满

超重

出发!!!</span>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值