Android学习笔记day11

第八天作业题

编程题:

1.定义一个乐器(Instrument)接口,其中有抽象方法

   void play();

InstrumentTest类中,定义一个方法

   void playInstrument(Instrument ins);

   并在该类的main方法中调用该方法。

要求:分别使用下列内部类完成此题。

成员内部类

局部内部类

匿名类

package com.xiaofei.zuoye;

//使用成员内部类实现

interface instrument{

public abstract void play();

}

class InstrumentTest{

class Inner1 implements instrument{

public void play() {

System.out.println("开始奏乐");

}

}

public void playInstrument(Inner1 ins){

ins.play();

}

}

public class zuoye9 {

 

public static void main(String[] args) {

InstrumentTest test  = new InstrumentTest();//创建外部类对象

InstrumentTest.Inner1 inner = test.new Inner1();//创建内部类对象

test.playInstrument(inner);

}

}

 

//使用局部内部类实现

package com.xiaofei.zuoye;

interface Instrument{

public abstract void play();

}

class InstrumentTest{

public void tell(){

class Piano implements Instrument{

public void play() {

System.out.println("开始奏乐");

}

}

Instrument piano = new Piano();

piano.play();

}

public void playInstrument(Instrument ins){

ins.play();

}

}

public class demo01 {

 

public static void main(String[] args) {

InstrumentTest test = new InstrumentTest();

test.tell();

}

}

 

package com.xiaofei.zuoye;

//使用匿名内部类实现

interface instrument{

public abstract void play();

}

class InstrumentTest{

public void playInstrument(instrument ins){

ins.play();

}

}

public class demo01 {

 

public static void main(String[] args) {

InstrumentTest test = new InstrumentTest();

test.playInstrument(new instrument(){

 public void play(){

System.out.println("开始奏乐");

 }

}

);}

}

2、定义一个FatherChild类,并进行测试。 

要求如下: 

1Father类为外部类,类中定义一个私有的String类型的属性namename的值为“zhangjun” 2Child类为Father类的内部类,其中定义一个introFather()方法,方法中调用Father类的name属性。 

3)定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用introFather ()方法。 

package com.xiaofei.zuoye;

class Father{

private String name = "张军";

class Child{

public void introFather(){

System.out.println("父亲的名字:"+name);

}

}

}

public class demo02 {

 

public static void main(String[] args) {

Father child = new Father();

Father.Child test = child.new Child();

test.introFather();

}

}

String类和StringBuffer类

1、位于java.lang包中

2、String类中的对象一旦被初始化就不能被改变。

3、StringBuffer用于封装内容可以改变的字符串。

用toString方法转换为String类型。

String x = “a” +4+“c”;编译时等效于:

String x= new StringBuffer().append("a").append(4).append("c").toString();

4、字符串常量实际上是一种匿名的String对象。

String类的常用成员方法:

1、构造方法:

String(byte[] bytes,int offset,int length)

equalsgnoreCase方法(判断字符串不区分大小写)

indexOf(int ch)(输出字母第一次出现的位置(可自己定义其实位置))

substring(int beginIndex)方法:输出某个位置之后的字符             substring(int beginIndex,int endIndex)方法:输出某两个字符之间的字符串

StringBuilder可变字符序列,线程不安全,效率高(类似于StringBuffer(线程安全,效率低))

append(累加,字符串相连)

replace(char,char)      新字符替换老字符

split(切割)

trim()去除字符串首尾的空格。

toCharArray返回新的字符数组。

equal比较两个字符串是否相等。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值