1.break和continue区别
使用场合:
break可用于switch结构和循环结构中。
continue只能用于循环结构中。
作用:
break语句终止某个循环,程序跳转到循环块外的下一条语句中。
continue跳出本次循环,进入下一次循环。
package qufen; import java.util.*;//相当于导入了所有Java.util下的所有包。 public class Over { public static void main(String[] args){ Scanner in=new Scanner(System.in); while(true){ System.out.println("请输入你的密码:"); int password=in.nextInt(); if(password!=428) continue;//结束该句后,执行下一次循环。 else break;//结束,直接跳出循环。 } System.out.println("密码正确。");} }2.日历查询
package qufen; import java.util.*;//相当于导入了Java包。 public class Over { public static void main(String[] args){ while(true){ Scanner in=new Scanner(System.in); System.out.println("请输入年份:"); int year=in.nextInt(); System.out.println("请输入月份:"); int month=in.nextInt(); int sum=0; for(int i=1900;i<=year-1;i++){ /*计算某天星期几,算出现在时间和1900年的时间差,总时间%7即求得星期几 * 总时间先求出从1900到前一年年末的时间然后加上这一年时间 * */ if(i%4==0&&i0!=0||i@0==0)//判断是否是闰年 sum+=366; else sum+=365; } for(int i=1;i<=month-1;i++){ if(i==2){ if(year%4==0&&year0!=0||year@0==0) sum+=29; else sum+=28; } else{ if(i==4||i==6||i==9||i==11) sum+=30; else sum+=31; } } int day; /* * 判断某个月有多少天*/ if(month==2){ if(year%4==0&&year0!=0||year@0==0) day=29; else day=28; } else{ if(month==4||month==6||month==9||month==11) day=30; else day=31; } sum+=1; int week=sum%7; System.out.println("日\t一\t二\t三\t四\t五\t六"); for(int j=1;j<=week;j++){ System.out.print("\t");//通过取余求出所需空的格数 } for(int m=1;m<=day;m++){ if(sum%7==6){ System.out.print(m+"\n");//如果星期六则需要换行 }else{ System.out.print(m+"\t"); } sum+=1; } System.out.println(); System.out.println("\n是否继续查询(yes or no):"); String p=in.next(); String s="yes"; if(p.equals(s))/* 比较两者是否相等,==比较时要地址相同 equals比较规则为:如果两个对象的类型一致,并且内容一致,则返回true,这些类有: java.io.file,java.util.Date,java.lang.string,包装类(Integer,Double等) */ continue; else break; } } } 3.父类引用指向子类对象
package lei; class Fu { int num=5; static int numS=6; void method1() { System.out.println("f1"); } void method2() { System.out.println("f2"); } static void method4() { System.out.println("f4"); } } class Zi extends Fu { int num=8; static int numS=9; void method1() { System.out.println("z1"); } void method3() { System.out.println("z3"); } static void method4() { System.out.println("z4"); } } class Test { public static void main(String[] args) { //- - - - - - - - - - - - - - - - - //例1:继承 //Zi z=new Zi(); //本类引用指向本类对象 //z.method1(); //Zi类复写method1,返回z1 //z.method2(); //Zi类继承Fu类method2,返回f2 //z.method3(); //Zi类特有method3,直接返回z3 //- - - - - - - - - - - - - - - - - //例2:多态 //Fu f=new Zi(); //父类引用指向子类对象 //f.method1(); //Zi类复写method1,返回z1 //f.method2(); //Zi类继承Fu类method2,返回f2 //f.method3(); //Fu类没有method3,编译失败 //- - - - - - - - - - - - - - - - - //例3,面试 //Fu f=new Zi(); //System.out.println(f.num); //先在Fu类找num属性,直接返回5 //Zi z=new Zi(); //System.out.println(z.num); //先在Zi类找num属性,直接返回8 //- - - - - - - - - - - - - - - - - //例4,面试 //Fu f=new Zi(); //f.method4(); //先在Fu类找methdo4,返回f4 //Zi z=new Zi(); //z.method4(); //先在Zi类找methdo4,返回z4 //- - - - - - - - - - - - - - - - - //例5,面试 //Fu f=new Zi(); //System.out.println(f.numS); //先在Fu类找numS,返回6 //Zi z=new Zi(); //System.out.println(z.numS); //先在Zi类找numS,返回9 } } 4 this和super class Person { public static void prt(String s) { System.out.println(s); } Person() { prt("A Person."); } Person(String name) { prt("A person name is:" + name); } } public class Chinese extends Person { Chinese() { super(); // 调用父类构造函数(1) prt("A chinese.");// (4) } Chinese(String name) { super(name);// 调用父类具有相同形参的构造函数(2) prt("his name is:" + name); } Chinese(String name, int age) { this(name);// 调用当前具有相同形参的构造函数(3) prt("his age is:" + age); } public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("kevin"); cn = new Chinese("kevin", 22); } } 结果为:A Person. A chinese. A person name is:kevin his name is:kevin A person name is:kevin his name is:kevin his age is:22 在这段程序中,this和super不再是像以前那样用“.”连接一个方法或成员,而是直接在其后跟上适当的参数,因此它的意义也就有了变化。 super后加参数的是用来调用父类中具有相同形式的构造函数,如1和2处。this后加参数则调用的是当前具有相同参数的构造函数,如3处。 当然,在Chinese的各个重载构造函数中,this和super在一般方法中的各种用法也仍可使用,比如4处,你可以将它替换为“this.prt” (因为它继承了父类中的那个方法)或者是“super.prt”(因为它是父类中的方法且可被子类访问),它照样可以正确运行。但这样似乎就有点画蛇添足的味道了。 super和this的异同: 1)super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) 2)this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句) 3)super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名 super.成员函数据名(实参) 4)this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名) 5)调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。 6)super()和this()类似,区别是,super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。 7)super()和this()均需放在构造方法内第一行。 8)尽管可以用this调用一个构造器,但却不能调用两个。 9)this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。 10)this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。 11)从本质上讲,this是一个指向本对象的指针, 然而super是一个Java关键字。
5多态
package h; public class fg{ public static void main(String[] args){ //向上类型转换 Cat cat = new Cat(); Animal animal = cat; animal.sing();//向上类型调用的也是子类方法 //向下类型转换 Animal a = new Cat(); Cat c = (Cat)a; c.sing(); c.eat(); //编译错误 //用父类引用调用父类不存在的方法 //Animal a1 = new Cat(); //a1.eat(); //编译错误 //向下类型转换时只能转向指向的对象类型 //Animal a2 = new Cat(); //Cat c2 = (Dog)a2; } } class Animal { public void sing() { System.out.println("Animal is singing!"); } } class Cat extends Animal { public void sing() { System.out.println("Cat is singing!"); } public void eat() { System.out.println("Cat is eating!"); } } /*结果:Cat is singing! Cat is singing! Cat is eating! */ /*面向对象的三大特性:封装、继承、多态。 多态的定义:指允许不同类的对象对同一消息做出响应。即同一消息可以根据发送对象的不同而采用多种不同的行为方式。(发送消息就是函数调用) * 多态存在的三个必要条件 一、要有继承; 二、要有重写; 三、父类引用指向子类对象。 * */ 6获取某个字符串出现次数 //获取某个字符串出现次数 String s = "sdsjavajdhsjajavajsdhjavdjava99;[javahdsjava"; String m = "java"; int count = 0; int index = -1; while(s.indexOf(m)!=-1){ index = s.indexOf(m);//获取指定字符串第一次出现的索引,如一开始index = 3; s=s.substring(index + m.length());//返回一个新字符串 count++; } System.out.println(count); }