class Person
{
int age;
String name;
Person(){}
Person(int age,String name)
{
//super();
this.age = age;
this.name = name;
}
public boolean equals(Object obj)
{
if(obj instanceof Person)
{
Person person = (Person)obj;
return this.age==person.age;
}
else
return false;
}
public String toString()
{
return name+","+age;
//return this.getClass().getName()+"@"+Integer.toHexString(this.hashCode());
}
//比较两个人是否是同龄人
/*
public boolean isSameAge(Person person)
{
return this.age==person.age;
}
*/
}
class Demo1
{
public static void main(String[] args)
{
//public boolean equals(Object obj)//Object obj =new Person(22);
Person p1 = new Person(24,"李四");
Person p2 = new Person(22,"张三");
//System.out.println(p1.equals(p2));
//System.out.println(p1==p2);
System.out.println(p1);//Person@16dadf9
System.out.println(p1.toString());
System.out.println(Integer.toHexString(p1.hashCode()));
Class claz = p1.getClass();//Person.class看成对象--Class
System.out.println(claz.getName());
}
}
//模板设计模式:定义一个功能时,功能的一部分是确定的,另外一部分是不确定的,确定的还会用到不确定的部分,
//那么就把不确定的部分暴露出去,让子类去实现
//需求:获取一个程序的运行时间
//得到程序运行的开始和结束时间,两个时间相减
abstract class Tool
{
public final void getTime()
{
long start = System.currentTimeMillis();
fun();
long end = System.currentTimeMillis();
System.out.println("程序运行时间是:"+(end-start)+"毫秒 ");
}
public abstract void fun();
}
class Zi extends Tool
{
public void fun()
{
for(int i=1;i<=3000;i++)
{
System.out.println("Hello World!"+i);
}
}
}
class Demo2
{
public static void main(String[] args)
{
Zi z = new Zi();
z.getTime();
}
}
/*
内部类:在一个类内部定义的类
内部类可以直接访问外部类中的成员
外部类使用 内部类的成员,需要先创建内部类的对象,再调用其成员
*/
class Outer
{
int num = 5;
class Inner//属于外部类的一个成员,成员内部类
{
public void show()
{
System.out.println("inner"+num);
}
}
public void fun()
{
Inner in = new Inner();
in.show();
}
}
class Demo3
{
public static void main(String[] args)
{
Outer out = new Outer();
out.fun();
//直接创建内部类对象
Outer.Inner in = new Outer().new Inner();
in.show();
}
}
class Outer
{
static int num = 5;
static class Inner//属于外部类的一个成员,成员静态内部类
{
public static void show()//内部类中含有静态成员,那么内部类必须是静态的
{
System.out.println("inner"+num);
}
}
public void fun()
{
Inner in = new Inner();
in.show();
}
}
class Demo4
{
public static void main(String[] args)
{
//访问静态内部类中的非静态方法
Outer.Inner in = new Outer.Inner();
in.show();
//访问静态内部类中的静态方法
Outer.Inner.show();
}
}
// 内部类可以直接访问外部类中的成员的原因:内部类持有外部类的引用 这个引用是:外部类名.this
//什么情况下用内部类?当描述事物时,在事物的内部还有事物需要描述,这时候就需要定义内部类
/*
class Person
{
class Heart
{
}
}
class Heart
{
Person person;
}
*/
class Outer
{
int num = 5;
class Inner
{
int num = 6;
public void show()
{
int num = 7;
System.out.println("inner"+Outer.this.num);
}
}
public void fun()
{
Inner in = new Inner();
in.show();
}
}
class Demo5
{
public static void main(String[] args)
{
Outer out = new Outer();
out.fun();
}
}
//局部内部类:访问了其所在函数的局部变量,该变量则必须是final的
class Outer
{
Object obj;
public void fun(final int x)
{
final int y = 6;
class Inner
{
public void show()
{
System.out.println("inner" +x);
}
public String toString()
{
return "inner"+y;
}
}
obj = new Inner();
obj.show();
}
public void function()
{
obj.toString();
}
}
class Demo6
{
public static void main(String[] args)
{
Outer out = new Outer();
out.fun();
out.function();
}
}
//匿名内部类:前提条件:继承或实现
abstract class Test
{
public abstract void show();
}
class Outer
{
class Inner extends Test
{
public void show()
{
System.out.println("Hello");
}
public void eat()
{
System.out.println("吃肉");
}
}
public void fun()
{
Inner in = new Inner();//创建了一个Test的子类对象
in.show();
in.eat();
// 创建了一个Test的子类对象
new Test(){
public void show()
{
System.out.println("Hello");
}
public void eat()
{
System.out.println("吃肉");
}
}.show();
new Test(){
public void show()
{
System.out.println("Hello");
}
public void eat()
{
System.out.println("吃肉");
}
}.eat();
//父类类型的引用指向了子类对象
Test t = new Test(){
public void show()
{
System.out.println("Hello");
}
public void eat()
{
System.out.println("吃肉");
}
};
t.show();
//t.eat();//父类中没有该方法,不能调用
System.out.println(new Object(){
String name = "李四";
int age = 23;
public String toString()
{
return name+","+age;
}
}.toString());
}
}
class Demo7
{
public static void main(String[] args)
{
Outer out = new Outer();
out.fun();
//ff(new Demo());
ff(new inter(){
public void sleep()
{
System.out.println("sleep");
}
});
}
public static void ff(inter in)//inter in = new Demo();
{
in.sleep();
}
}
class Demo implements inter
{
public void sleep()
{
System.out.println("sleep");
}
}
interface inter
{
public void sleep();
}
interface inter
{
public void show();
}
class Test
{
public static inter methord()
{
return new inter(){
public void show()
{
System.out.println("show");
}
};
}
}
class Demo8
{
public static void main(String[] args)
{
Test.methord().show();
}
}
//内部类的应用场景
interface OnClickListener
{
public void onClick();
}
class Button
{
boolean isClick = true;
public void setOnClickListener(OnClickListener onClickListener)
{
if(isClick)
onClickListener.onClick();
else
System.out.println("按钮没有被点击");
}
}
class Demo9
{ //成员内部类
class ButtonClick implements OnClickListener
{
public void onClick()
{
System.out.println("按钮被点击");
}
}
public void onCreate()
{
// Button but = new Button();
//but.setOnClickListener(new ButtonClick());
//局部内部类
class ClickButton implements OnClickListener
{
public void onClick()
{
System.out.println("按钮被点击");
}
}
//Button but = new Button();
// but.setOnClickListener(new ClickButton());
//匿名内部类
Button but = new Button();
but.setOnClickListener(new OnClickListener(){
public void onClick()
{
System.out.println("按钮被点击");
}
});
}
public static void main(String[] args)
{
Demo9 d = new Demo9();
d.onCreate();
}
}
/*
异常:程序在运行时出现的不正常情况
异常的由来:java把程序在运行时出现的各种不正常情况也看成对象,提取属性和行为进行描述,(比如异常名字,异常信息,异常出现的位置)
从而形成了各种异常类,
异常的分类:
一类是严重的问题:Error类进行的描述
一般不用写针对性的代码处理
一类是不严重的问题:Exception类进行描述
一般写针对性的代码处理
异常体系:
Throwable
---Error
---Exception
*/
class Demo10
{
public static void main(String[] args)
{
//int[] arr = new int[1024*1024*200];//OutOfMemoryError:
int[] arr = new int[3];
//这种数组下标越界异常,因为在java 内部已经定义好了,所以当出现这种异常时,系统会自动创建该异常类对象
//因为main函数处理不了该异常,所以抛给了jvm,jvm默认的处理方式是调用异常类对象的printStackTrace()方法,打印
//异常 名称,异常信息,异常出现的位置,然后中断程序,后边的代码不会被执行
System.out.println(arr[3]);//throw new ArrayIndexOutofBoundsException(),默认是jvm处理异常
System.out.println("hahahhahhaha");
}
}
class MyMath
{
public int div(int a,int b)
{
return a/b;//throw new ArithmeticException();//内部自动创建异常类对象,抛给main函数
}
}
class Demo11
{
public static void main(String[] args)
{
MyMath m = new MyMath();
int result = m.div(2,0);//throw new ArithmeticException() 抛给jvm
System.out.println(result);
}
}
/*
try{
可能发生异常的代码
}
catch(异常类 参数名)
{
处理异常的代码
}
*/
class MyMath
{
public int div(int a,int b)
{
return a/b;//throw new ArithmeticException();
}
}
class Demo12
{
public static void main(String[] args)
{
MyMath m = new MyMath();
try
{
int result = m.div(2,0);//new ArithmeticException()
System.out.println(result);
}
catch (Exception e)//Exception e = new ArithmeticException();
{
// System.out.println("被除数为0了");
System.out.println(e.getMessage());//异常信息
System.out.println(e.toString());//异常名称:异常信息
e.printStackTrace();//异常名称:异常信息 异常发生的位置
}
System.out.println("haha");
}
}
/*
在函数后边使用throws声明抛出异常:
那么调用者必须处理:
处理方式有两种:
1:使用try{}catch(){}处理
2:继续使用throws声明抛出
*/
class MyMath
{
public int div(int a,int b)throws Exception//声明该方法可能发生异常,强制调用该方法的调用者必须处理异常,把运行时期的问题转移到了编译时期
{
return a/b;
}
}
class Demo13
{
public static void main(String[] args)throws Exception
{
MyMath m = new MyMath();
//try{
int result = m.div(2,2);
System.out.println(result);
//}catch(Exception e){}
System.out.println("haha");
}
}
class MyMath
{
public int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
{
int[] arr = new int[4];
System.out.println(arr[2]);
return a/b;
}
}
class Demo14
{
public static void main(String[] args)throws Exception
{
MyMath m = new MyMath();
try{
int result = m.div(2,0);
System.out.println(result);
}
catch(ArithmeticException e)
{
System.out.println("被除数为0了");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("下标越界了");
}
/*
catch(Exception e)
{
}
*///子类异常要写在父类异常的上边
System.out.println("haha");
}
}
/*
自定义异常:
被除数为负数了也认为是异常
在函数内部使用throw抛出了异常,必须进行处理,处理方式有两种
1:使用try{}catch(){}处理
2:继续使用throws声明抛出
throw 和throws的对比
throw后边跟的是异常类对象 // this.message = message;
throws后边跟的是异常类
*/
class FuShuException extends Exception
{
private int num ;
public FuShuException()
{
super();
}
public FuShuException(String message)
{
super(message);
}
public FuShuException(String message,int num)
{
super(message);
this.num = num;
}
public int getNum()
{
return this.num;
}
}
class MyMath
{
public int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("被除数为负数了",b);//这种异常是自定义的,必须手动创建该异常类对象,因为系统不知道该异常,不会自动创建
//if(b==0)
//throw new ArithmeticException();
return a/b;//throw new ArithmeticException();
}
}
class Demo15
{
public static void main(String[] args) //throws FuShuException
{
MyMath m = new MyMath();
try
{
int result = m.div(2,-2);//new FuShuException()
System.out.println(result);
}
catch (ArithmeticException e)
{
System.out.println("被除数为0了");
}
catch(FuShuException e)
{
System.out.println(e.getMessage()+"这个负数是:"+e.getNum());
}
}
}
/*
在函数后声明抛出异常,调用者不用处理,编译照样通过
在函数内使用throw抛出异常类对象,不用处理,编译照样通过
因为异常类中有一个子类RuntimeException(运行时异常) ,如果发生的异常是该异常或该异常子异常,那么就是以上两种情况
异常类分为两类:
编译时检测的异常:非RuntimeException类及其子类
编译时不检测的异常:RuntimeException类及其子类
对于运行时异常,java认为遇到这种异常,程序就该中断,不应该处理
运行时异常通常都是由于调用者传递了不合法的数据造成的,程序就该中断,去修改数据
*/
class FuShuException extends Exception
{
private int num ;
public FuShuException()
{
super();
}
public FuShuException(String message)
{
super(message);
}
public FuShuException(String message,int num)
{
super(message);
this.num = num;
}
public int getNum()
{
return this.num;
}
}
class MyMath
{
public int div(int a,int b)
{
if(b==0)
throw new ArithmeticException();
return a/b;
}
}
class Demo16
{
public static void main(String[] args)
{
MyMath m = new MyMath();
int result = m.div(2,0);
System.out.println(result);
}
}
//运行时异常举例:
//获取一个数组中指定下标的值 结果:一个值 参数:数组,下标
class Test
{
public int getNum(int[] arr,int index)
{
if(index>=arr.length || index<0)
throw new ArrayIndexOutOfBoundsException();
if(arr == null)
throw new NullPointerException();
return arr[index];
}
}
class Demo17
{
public static void main(String[] args)
{
Test t= new Test();
int[] arr={34,2,56,7};
//arr = null;
t.getNum(arr,2);// throw new ArrayIndexOutOfBoundsException();
}
}
/*
try
{
}
catch
{
}
finally
{
必须被执行的代码
}
*/
class MyMath
{
public int div(int a,int b)
{
return a/b;
}
}
class Demo18
{
public static void main(String[] args)
{
MyMath m = new MyMath();
try{
int result = m.div(2,0);
System.out.println(result);
}catch(Exception e)
{
System.out.println("除数为0了");
return;
//System.exit(1);
}
finally
{
System.out.println("finally");
}
System.out.println("haha");
}
}
/*
try
{
}
catch()
{
}
try
{
}
catch
{
}
finally
{
必须被执行的代码
}
try
{
}
finally
{
}
*/
class Demo19
{
public static void main(String[] args)
{
try
{
//使用资源
}
finally
{
//释放资源
}
/*
try
{
throw new Exception();
}
catch (Exception e)
{
try
{
throw e;
}
catch (Exception e )
{
throw new RuntimeException();
}
}*/
}
}
//异常应用:老师用电脑上课
//蓝屏,冒烟
class LanPing extends Exception
{
public LanPing(){
super();
}
public LanPing(String message){
super(message);
}
}
class MaoYan extends Exception
{
public MaoYan(){
super();
}
public MaoYan(String message){
super(message);
}
}
class ClassProgressException extends Exception
{
public ClassProgressException()
{
super();
}
public ClassProgressException(String message)
{
super(message);
}
}
class Computer
{
int state = 3;
public void run()throws LanPing,MaoYan
{
if(state==2)
throw new LanPing("电脑蓝屏了");
else if(state==3)
throw new MaoYan("电脑冒烟了");
else
System.out.println("电脑运行");
}
public void reset()
{
System.out.println("电脑重启");
state =1;
}
}
class Teacher
{
private String name;
private Computer computer;
public Teacher(){}
public Teacher(String name)
{
this.name = name;
computer = new Computer();
}
public void teach()throws ClassProgressException
{
try
{
computer.run();
System.out.println(name+"上课");
}
catch (LanPing e)
{
System.out.println(e.toString());
computer.reset();
}
catch(MaoYan e)
{
test();
throw new ClassProgressException("上课进度受影响了");
}
System.out.println(name+"继续上课");
}
public void test()
{
System.out.println("学生练习");
}
}
class Demo20
{
public static void main(String[] args)
{
Teacher teacher = new Teacher("小黑老师");
try
{
teacher.teach();
}
catch (ClassProgressException e)
{
System.out.println(e.toString());
System.out.println("学生练习,老师休息");
}
}
}
/*
计算一个长方形和一个圆形的面积,面积小于0视为异常
*/
class AreaFeiFaException extends RuntimeException
{
public AreaFeiFaException(){
super();
}
public AreaFeiFaException(String message){
super(message);
}
}
abstract class Shape
{
public abstract double getArea();
}
class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle()
{}
public Rectangle(double length,double width)
{
if(length<0 || width<0)
throw new AreaFeiFaException("长或宽的值小于0了");
this.length = length;
this.width = width;
}
public double getArea()
{
return this.length*this.width;
}
}
class Demo21
{
public static void main(String[] args)
{
Rectangle r = new Rectangle(23,2);
double area = r.getArea();
System.out.println("area="+area);
}
}
//字符串:
class Demo22
{
public static void main(String[] args)
{
//字符串是常量;它们的值在创建之后不能更改
String s1 = "hello";// 字符串是存在常量池
s1 = "world";
//特殊情况
/*
在定义s2时,先去常量池中看是否有字符串"1000phone",没有的话创建该字符串,结果是没有
在定义s3时,先去常量池中看是否有字符串"1000phone",结果有,有的话就使用已有的
*/
String s2="1000phone";
String s3="1000phone";
System.out.println(s2==s3);//true
String s4 = new String("1000phone");
System.out.println(s2==s4);//false
// String重写了Object中的boolean equals(Object obj)方法,重写之后的功能是比较两个字符串的内容是否一样
System.out.println(s2.equals(s4));
System.out.println("haha".equals("hehe"));
//面试题:下面两句话分别含有几个对象
String s4 = new String("1000phone");
String ss = "1000phone";
}
}
/*
获取:
获取字符串的长度
int length()
获取某一个位置上的字符
char charAt(int index)
获取字符在字符串中的位置
如果要找的字符或者字符串不存在,返回值为-1
int indexOf(char ch)//返回字符在字符串中第一次出现的位置
int indexOf(int ch, int fromIndex)//第二个参数用于指定开始找的位置
int indexOf(String str) //获取一个字符串在字符串中出现的位置
int indexOf(String str, int fromIndex)//第二个参数用于指定开始找的位置
int lastIndexOf(char ch)//最后一次出现的位置
*/
class Demo23
{
public static void main(String[] args)
{
print("owi".length());
print("hello world".charAt(6));
print("hello world web".indexOf('w'));
print("hello world web".indexOf('w',7));
print("hello world".indexOf("world"));
print("hello world hello world".indexOf("world",7));
print("hello world hello world".lastIndexOf("world"));
//要找的字符或字符串不存在
print("abcdefg".indexOf('w'));
String mail ="abc@126.com";
boolean boo = checkMail(mail);
print(boo);
}
//验证邮箱格式是否合法
public static boolean checkMail(String mail)
{
if(mail!=null && mail!="")
{
if(mail.indexOf('@')==-1)
return false;
else if(mail.indexOf('.')==-1)
return false;
else if(mail.indexOf('@')>mail.indexOf('.'))
return false;
else
return true;
}
else
return false;
}
public static boolean checkLogin(String userName,String passWord)
{
if(userName!=null && userName!="" && passWord!=null && passWord!="")
{
if("admin".equals(userName) && "123".equals(passWord))
{
return true;
}
else
return false;
}
else
return false;
}
public static void print(Object obj)
{
System.out.println(obj);
}
}