java学习笔记(九)

来源:互联网 发布:pdf to word for mac 编辑:程序博客网 时间:2024/05/20 21:18

一、接口(interface)

1.接口:全部的方法都是抽象的方法,全部的属性都是常量。接口用来表示纯抽象概念,没有任何具体的方法和属性。

2.不能实例化,可定义变量

3.接口变量可以引用具体实现类的实例

4.接口只能被实现,一个具体类实现接口必须使用全部的抽象方法

5.接口之间可继承

6.一个具体类可实现多个接口,实现多继承现象,表示:一个概念既是Xxx也是Xxx

7.接口中的属性默认是常量,public static final

8.接口的方法一定是public abstract

9.实现一个接口,使用关键字implements,实现实际上是一种继承关系,接口和实现类是父子类型关系

例子:

/** * 交通工具概念,定义接口 */public interface Vehicle {/**当前交通工具类型, * 0代表汽车,1代表自行车,2代表公共汽车*/<span style="white-space:pre"></span>//接口中的属性默认为public static final<span style="white-space:pre"></span>int CAR=0;<span style="white-space:pre"></span>int BICYCLE=1;<span style="white-space:pre"></span>int BUS=2;<span style="white-space:pre"></span>//接口中默认方法都是public abstract<span style="white-space:pre"></span>int getType();<span style="white-space:pre"></span>void run();<span style="white-space:pre"></span>void stop();}
/** * 抽象的汽车,部分具体,部分抽象 * getType(),run()具体的方法 * stop()方法没有实现,是抽象的,由继承Car的类具体实现 */public abstract class Car implements Vehicle {public int getType(){return CAR;}public void run(){System.out.println("给油");}}
/** * 奥拓汽车,是具体汽车,继承Car */public class Auto extends Car{public void stop(){System.out.println("机械式刹车");}}
/** * 跑车是一种交通工具 */public interface Roadster extends Vehicle{int getMaxSpeed();}
/** * AudiR8是汽车也是跑车 *Vehicle可以不写,因为Car已经实现了Vehicle */public class AudiR8 extends Car implements Roadster, Vehicle{public void stop() {//VehicleSystem.out.println("能量回收式刹车");}public int getMaxSpeed() {//Roadsterreturn 380;}}
public class VehicleDemo {public static void main(String[] args) {//Vehicle v=new Vehicle();抽象的不能new//接口订立了规约,由具体类实现Vehicle v;v=new Bicycle();v.run();v.stop();v=new Auto();v.run();v.stop();v=new AudiR8();v.run();v.stop();System.out.println(((AudiR8) v).getMaxSpeed());}}

二、java.lang.Object

Object是Java的继承root类,所有类都从这个类继承

1.toString()方法:

toString()方法返回对象的文本描述,经常被系统默认调用,默认返回:全限定名@HashCode

建议覆盖toString(),返回合理的文本

public class Point{  int x;  int y;  public Point(int x,int y){    this.x=x;    this.y=y;  }  public String toString(){    return "("+x+","+y+")";  }}
2.equals()方法

equals用来比较两个对象是否相等

(1)区别:引用相等与对象相等

(2)引用相等使用“==”

(3)对象相等 .equals()方法

(4)equals在Object类中声明,默认的比较规则是:引用比较

(5)建议覆盖,实现对象的比较(比较对象的状态就是比较对象的数据)

public boolean equals(Object obj){if(obj==null)return false;if(this==obj)//地址一样return true;if(obj instanceof Point){Point other=(Point)obj;//类型转换return x==other.x&&y==other.y;}return false;}

(6)hashCode()方法要与equals()方法一同覆盖

·当两个对象equals比较为true时,这两个对象应具有相同的hashCode()值

·当两个对象equals比较为false时,这两个对象应该具有不同的hashCode()值

·默认的hashCode()值是当前对象地址转换的一个整数,这个整数不是内存地址

//hashCode和equals一同覆盖,保证当equals返回false时hashCode不同,否则相同public int hashCode(){return x*10000+y;}

三、String字符串

1.字符串表示:char[],String,StringBuilder

2.字符串“字面量”都是String类型实例

3.String内部封装了一个不变的char[]

4.String API 有一个实现原则:对象内容永远不变,也就是String对象永远不变

String对象方法返回的String是一个新对象,toString()除外

<span style="white-space:pre"></span>//除了toString()方法以外,String中的其他的所有方法都是返回新字符串,原字符串不变String s3=s1.concat("123");//连接System.out.println(s1);//原对象System.out.println(s3);//新对象String s4=s3.toUpperCase();//小写变大写,原对象不变,新对象改变System.out.println(s4);

String API:

Length(),charAt(),toUpperCase(),toLowerCase()

indexOf()从前向后查找出现的位置

lastIndexOf()从后向前查找出现的位置

substring()从指定位置向后截取字符串

startsWith()以。。开头

endsWith()以。。结尾

<span style="white-space:pre"></span>String file="photo.jpg.bak";if(file.indexOf(".jpg")>-1){System.out.println("图片文件");}if(file.endsWith(".jpg")){System.out.println("图片文件");}String name=file.substring(0, 9);//9不包括在内System.out.println(name);//photo.jpgString ext=file.substring(file.lastIndexOf("."));System.out.println(ext);

trim()去掉空白,\n,\t,\r都会被去掉

<span style="white-space:pre"></span>String login="  \t\ntom\r ";login=login.trim();System.out.println(login);//tom

5.String字面量(直接量)如果相同,会替换为同一个String对象的引用

<span style="white-space:pre"></span>//静态字符串String s1="abc";String s2="abc";String s3="ab"+"c";//abcSystem.out.println(s1==s2);//trueSystem.out.println(s1==s3);//true//动态字符串String s4=new String("abc");//运行到这的时候在堆中创建System.out.println(s4);System.out.println(s1==s4);//false

6.字符串常量连接(+)结果也被优化为一个字符串

7.String可使用equals比较对象内容相等

8.同样内容的字符串hashCode()值一样

9.字符串的字符序号从0开始



0 0
原创粉丝点击