接口
接口中的方法全部都是抽象的
接口写法:interface 接口名{}
实现接口:
public interface Inter {
abstract void print();
}
public class Demo implements Inter{//用 implements 实现接口
@Override
public void print() {
System.out.println("print");
}
}
接口也不能被实例化 只能用多态的方式去实例化
接口的子类可以是抽象类 不能实例化 需要抽象类的子类 用多态的方法才能实例化
public interface Inter {
public void print();//会隐式自动加上 abstract
int a=10;//接口里定义的变量都会变成常量 底层会自动加上public static final
//一般接口中很少申明变量
}
接口定义变量
public interface Inter {
//定义接口变量 会变成常量 不可以更改
int num=10;//默认是 public static final int num=10; 开发过程中建议手动给出
}
总结:
抽象类:
成员变量: 可以是变量也可以是常量
构造方法:有(被子类初始化使用的)
成员方法:可以抽象 也可以非抽象
抽象类和普通类差不多 该怎么定义就怎么定义 只是增加了强制子类去重写的抽象方法
设计理念:抽象类被继承体现的是 is it 的关系 抽象类中定义的是该继承体系的共同功能
接口:
成员变量:是常量 public static final int num=10;
成员方法: 只可以抽象
类与接口:实现关系 可以单实现 也可以多实现 并且可以再继承一个类的同时实现多个接口
接口与接口:继承关系 可以单继承 也可以多继承
设计理念:接口被实现体现的是 like it 的关系 接口中定义的是该继承体系的扩展功能
内部类:
public class Outer {
public static void main(String[] args){
// Inter in=new Inter(); 报错 不能实例化
//内部类创建实例化的方式一:
Outer.Inter in=new Outer().new Inter();
in.getMsg();
//内部类创建实例化的方式二:
Outer ou=new Outer();
Outer.Inter it1=ou.new Inter();
it1.getMsg();
}
private int a=10;
int b=20;
public void show(){
System.out.println("外部类中的show");
}
//成员内部类
class Inter{
int c=30;
public void getMsg(){
System.out.println("a:"+a);//在内部类中可以访问外部类中的私有成员属性
System.out.println("b:"+b);
show();
}
}
}
API
Object(equals(),hashCode(),toString(),getClass(),finalize())
object中的hashCode():返回该对象的哈希码值 默认情况下 该方法会根据对象的地址值计算
不同对象hashCode()一般来说不会相同 但是同一个对象的hashCode()值肯定相同
public class Worker {
private String name;
private int no;
public Worker() {
}
public Worker(String name, int no) {
this.name = name;
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Worker{" +
"name='" + name + '\'' +
", no=" + no +
'}';
}
public void work(){
System.out.println("工作");
}
//当内存中有足够的垃圾 需要被回收时这个方法就会被GC(垃圾回收机制)调用
protected void finalize() throws Throwable{
System.out.println("正在回收垃圾");
}
}
public class Demo01 {
public static void main(String[] args) {
Worker w1=new Worker();
Worker w2=new Worker();
//.equals()
boolean b=w1.equals(w2);
System.out.println(b);// false Object中的equals方法是比较对象的地址值
//.hashCode(),.toString()
System.out.println(w1);//地址:com.muzhou.Object.Worker@1b6d3586
System.out.println(w1.hashCode());//哈希码值:460141958
//打印w1和w1.toString() 对象引用时实际上默认调用了Object的toString()
System.out.println(w1.toString());//地址:com.muzhou.Object.Worker@1b6d3586
//--------------------------------------------------------------------------
//在Worker类中重写toString()
System.out.println(w1);//Worker{name='null', no=0}
System.out.println(w1.toString());//Worker{name='null', no=0}
//.getClass()
Class c=w1.getClass();//获取该对象的字节码文件
System.out.println(c);//class com.muzhou.Object.Worker
String name=c.getName();
System.out.println(name);//com.muzhou.Object.Worker
//finalize()
for(int i=1;i<1000000;i++){
new Worker();//匿名对象
}
}
}
String
public class Demo02 {
public static void main(String[] args) {
String s1="liao";//等同于String s=new String("liao");
System.out.println(s1.hashCode());//3321451
s1="辽宁";//因为等同于new String,liao就变成了垃圾 将辽宁赋值给s1
System.out.println(s1.hashCode());//1164132 两个哈希码值对比 不一样 说明已经不是同一个对象了
String s2=new String("abc");//问:在内存中创建了几个对象?
//答:两个 常量池一个,副本一个
}
}
public class Demo03 {
public static void main(String[] args) {
byte[] b={97,98,99,100};
String s=new String(b);//将字节数组转换为字符串
System.out.println("s="+s);//abcd a(97),b(98),c(99),d(100)
String s1=new String(b,1,2);//将字节数组的一部分转化为字符串 从1下标开始的2个字节
System.out.println("s1="+s1);//bc
char[] c={'d','e','m','o'};
String s2=new String(c);
System.out.println("s2="+s2);//demo
//问:打印输出的值是true还是false
String s3="辽宁";
String s4="辽宁";//在方法区
System.out.println(s3==s4);//true答:因为辽宁存储在常量池中 Jvm底层为了节约内存 如果常量池中存在同一个常量那么就直接拿过来用
System.out.println(s3.equals(s4));//true
String s5=new String("辽宁");//在堆内存
System.out.println(s4==s5);//false 因为s5 new 之后重新开辟了一个地址 这个比较的是地址值 所以结果为false
String s6="a"+"b"+"c"+"d";//字符串中 + 是连接符 拼接
String s7="abcd";
System.out.println(s6==s7);//true
System.out.println(s6.equals(s7));//true
String s8="ab";
String s9="abc";
String s10=s8+"c";
System.out.println(s9==s10);//false
System.out.println(s9.equals(s10));//true
}
}
String类中的判断功能
public class Demo04 {
public static void main(String[] args) {
String s1="abcD";
String s2="abcd";
System.out.println(s1.equals(s2));//false
//不区分大小写 s1.equalsIgnoreCase(s2)
System.out.println(s1.equalsIgnoreCase(s2));//true
//是否包含("a");
System.out.println(s1.contains("a"));//true
//是否以("b")开头
System.out.println(s1.startsWith("b"));//false
//是否以("D")结尾
System.out.println(s1.endsWith("D"));//true
//是否为空
System.out.println(s1.isEmpty());//false
//""这个属于字符串常量 可以直接当对象使用 所以可以调用String类中的方法
System.out.println("".isEmpty());//true
}
}
String类中的获取功能
public class Demo06 {
public static void main(String[] args) {
//String里面没有0下标!!!!!但是索引要带0 没有索引从1开始
String s1="木舟0基础学习Java的第十天";
//获取字符串的长度
int len=s1.length();
System.out.println(len);//15
//获取指定索引位置的一个字符
char c=s1.charAt(5);
System.out.println(c);//学
//获取"础"所在位置的索引
int index=s1.indexOf("础");
System.out.println(index);
//获取b(98)的下标
System.out.println("abcd".indexOf(98));//1
System.out.println("aecd".indexOf(98));//-1 没找到返回-1
//返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
System.out.println(s1.indexOf("a",0));//8
//返回指定字符在此字符串中最后一次出现处的索引
System.out.println(s1.lastIndexOf("a"));//10
//从指定位置截取字符串 默认到结尾
String s2=s1.substring(3);
System.out.println(s2);//基础学习Java的第十天
//从2~5截取字符串
String s3=s1.substring(2,5);//不包括5 其实就是2-4
System.out.println(s3);//0基础
}
}
模拟用户登录
public class Demo05 {
public static void main(String[] args) {
//模拟用户登录 用户名:admin 密码:admin
admin();
}
public static void admin(){
Scanner sc=new Scanner(System.in);
for(int i=3;i>0;i--){
System.out.println("请输入您的账号:");
String name=sc.next();
System.out.println("请输入您的密码:");
String mima=sc.next();
if("admin".equals(name)&&"admin".equals(mima)){
System.out.println("恭喜您"+name+"登录成功");
break;
}else{
System.out.println("您的用户名或密码有误,还有"+(i-1)+"次机会");
//退出程序
if(i==0){
System.exit(0);
}
}
}
}
}
堆和栈
堆:先进先出(队列模式)
存储字节码 静态数据 静态区 常量池 no_heap
栈:先进后出(弹夹)