0717 抽象类/接口/异常/==-equals()/搜索字符串/包装类型(统计字符数)/hashCode()/StringBuffer/pingSQL/2018年美式日历
抽象類與接口
? 什么是抽象類?
? 有一些類沒有必要實例化,實例化沒有意義,它可以封裝通用
? 的業(yè)務代碼
? Animal -- 沒有必要實例化
? Dog
? Cat
? Fish ...
? ?抽象類用abstract修飾
? ?抽象方法:用abstract修飾的方法,只有方法的聲明,沒有方法的實現(xiàn)
? ?在子類中實現(xiàn)抽象方法
? 一個子類繼承抽象類,就必須實現(xiàn)抽象中中聲明的所有抽象方法,否則
? 這個子類也必須聲明為抽象類
? 案例:寫Cat類,繼承Animal,測試
? 側(cè)重點在代碼重用
? 什么是接口?
? ?更加純粹的抽象類,將抽象類中非抽象的因素都剝離了,專注于
? ?頂層設計
? ?接口中只能由兩種成員
? ? ?a. public abstract 修飾的方法
? ? ?b. public static final 修飾的變量 -->常量
?側(cè)重點在設計對象
? ? ?一個類只能繼承一個一般類或者抽象類
? ? 但是一個類可以實現(xiàn)多個接口(變相的實現(xiàn)了多繼承)
? ? 接口可以繼承接口,而且是多繼承
? ? ?
異常
? ?什么是異常?
? ? ? 程序在運行是發(fā)生的錯誤
? ? ? 異常發(fā)生的時候虛擬機(JVM)會將異常信息封裝成對象
? ? ? 異常信息: a. 異常消息(對異常簡明扼要的描述)
? ? ? ? ? ? ? ? ? ? ?b.異常堆棧信息(異常因果的回溯)
? ?異常的體系結(jié)構(gòu)
? ? ? ? ? Throwable
? ? ? ? ? ?Error(程序不用處理)
? ? ? ? ? ?Exception
? ? ? ? ? ? ? ? --運行時異常 RuntimeException,可以處理,也可以不處理
? ? ? ? ? ? ? ? --檢查異常(除開運行時異常以外的所有異常),必須處理
?
? ?異常的處理
? ? ? 拋出:拋給異常發(fā)生代碼的調(diào)用者
? ? ? 捕獲:
? ? try{
? ? ? ?//異常可能發(fā)生的代碼
? ? }catch(Exception e){//e是異常的引用
? ? ? ?e.printStackTrace();//打印異常堆棧信息
? ?}finally{
? ? ? ? ?//不論異常發(fā)生與否,都必須執(zhí)行的代碼
? ? ? ? ?//一般用來釋放資源
? ?}
? ?自定義異常(針對具體業(yè)務邏輯)
? ?案例:年齡異常案例
java.lang包
包裝類型 (統(tǒng)計字符案例) ?
? 基本類型 ? ? 包裝類型
? ?byte ? ? ? ? ? ? ?Byte ? ? ? ? ? ??
? ?short ? ? ? ? ? ? Short ? ? ? ? ? ? ? ? ? ? ? ? ??
? ?int ? ? ? ? ? ? ? ? Integer
? ?long ? ? ? ? ? ? ?Long
? ?float ? ? ? ? ? ? ?Float
? ?double ? ? ? ? ?Double
? ?boolean ? ? ? ?Boolean
? ?char ? ? ? ? ? ? ?Character
案例:從控制臺輸入一個字符串
? ? ? ? "hello123ABC##@@"
? ? ? ? 統(tǒng)計由多少個大寫字母,小寫字母,數(shù)字,其他字符
??
? ? ? ? char ch = ?charAt(3)
? ? ? ? ?'a'=<ch<='z'
? ? ? ? ?'A'=<ch<='Z'
? ? ? ? ?'0'=<ch<='9'
? ? ? ? ?else..
Object(equals()方法)
? ?java根類,所有的類都直接或者間接繼承于Object類
? ? ==比較的是地址值
? ? equals()方法比較是對象的內(nèi)容
? ? 創(chuàng)建一個必須重寫從Object繼承過來的equals()方法,才能比較
? ? ?對象的內(nèi)容
? ? ?如何重寫equals()方法
String類型(indexOf ,lastIndexOf() )
(將輸入的多個英文名轉(zhuǎn)換為"Tom"的形式)
?tom TOM ?toM ...?
?字符串一但創(chuàng)建就不可改變,所有的拼接和修改都是創(chuàng)建新的對象
?如果頻繁的拼接或修改,會產(chǎn)生大量的中間對象,造成內(nèi)存溢出
StringBuffer(判斷回文,拼接sql)
? 可變字符串
java.util包
Date
? ?分裝了日期于時間的信息
? ?日期:年 月 日
? ?時間:時 分 秒 毫秒
? Date date = new Date();//當前系統(tǒng)時間
SimpleDateFormat?
? ?格式器:用來定義Date對象的格式
? ?yyyy -- 年
? ?MM -- 月
? ?dd ?-- 日
?
? ?HH 24小時制時
? ?hh ?12小時制時
? ?mm 分
? ?ss ?秒
Calendar (打印日歷,打印全年日歷)
? 打印當前月的日歷
?
抽象類?
package demo03;public abstract class Animal {//抽象類private String name;private int age;public Animal(){}public Animal(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}//eat() ---設計為抽象方法public abstract void eat();//sleep()..public abstract void sleep();} package demo03;public class TestAnimal {public static void main(String[] args) {//抽象類不能創(chuàng)建對象(實例化)//抽象類有沒有構(gòu)造方法? 有//留給子類的構(gòu)造方法調(diào)用//Animal an = new Animal();//抽象類可以創(chuàng)建上轉(zhuǎn)型對象Animal dog = new Dog("旺財",1);dog.eat();dog.sleep();Animal cat = new Cat("花花",2);cat.eat();cat.sleep();} } package demo03;public class Dog extends Animal{public Dog() {super();}public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("小狗"+getName()+"在啃骨頭");}@Overridepublic void sleep() {System.out.println("小狗"+getName()+"白天在睡覺"); }} package demo03;public class Cat extends Animal {public Cat() {super();}public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("小貓"+getName()+"在吃東西");}@Overridepublic void sleep() {System.out.println("小貓"+getName()+"在屋頂上睡覺");}}接口 |多繼承?
package demo04;public interface IEat {// public static final int a = 1 ;void eat(); } package demo04;public interface IFlying {void flying(); } package demo04;public interface ISleep {void sleep(); } package demo04;public interface ISwimming {void swimming(); } package demo04;public interface ISome extends IFlying ,ISwimming,IEat,ISleep{} package demo04;public class Cat implements IEat,ISleep{private String name;private int age;public Cat() {super();// TODO Auto-generated constructor stub}public Cat(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic void eat() {System.out.println("小貓"+this.name+"在吃魚");}@Overridepublic void sleep() {System.out.println("小貓"+this.name+"在屋頂上睡覺");}} package demo04;public class Bird implements ISome {@Overridepublic void flying() {//xxx}@Overridepublic void swimming() {// TODO Auto-generated method stub}@Overridepublic void eat() {// TODO Auto-generated method stub}@Overridepublic void sleep() {// TODO Auto-generated method stub}}?異常 try-catch
package demo05;public class Demo01 {public static void main(String[] args) {test();//java.lang.ArithmeticException}public static void test(){int a = 10;a = a/0; //除零異常} } package demo05;public class Demo02 {public static void main(String[] args) throws ClassNotFoundException {try {//可能發(fā)生異常的代碼Class.forName("java.lang.String");System.out.println("異常后的代碼");} catch (ClassNotFoundException e) {//處理異常的代碼e.printStackTrace();throw e;}System.out.println("try catch 后的代碼");} }??自定義異常 try-catch-finally
package demo05;public class Emp {private int id;private String name;private int age;private String sex;private float sal;private String dept;public Emp() {super();}public Emp(int id, String name, int age, String sex, float sal, String dept) throws AgeException {super();this.id = id;this.name = name;if(age<18 || age>60){throw new AgeException("合法年齡:18~60");}this.age = age;this.sex = sex;this.sal = sal;this.dept = dept;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) throws AgeException {if(age<18 || age>60){throw new AgeException("合法年齡:18~60");}this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public float getSal() {return sal;}public void setSal(float sal) {this.sal = sal;}public String getDept() {return dept;}public void setDept(String dept) {this.dept = dept;}}?
package demo05;public class TestEmp {public static void main(String[] args) {try {Emp e = new Emp(1,"eric",20,"男",20000,"java開發(fā)部");e.setAge(300);System.out.println("異常沒有發(fā)生");} catch (AgeException e) {e.printStackTrace();System.out.println("異常發(fā)生了");}finally{//不論異常發(fā)生與否,都必須執(zhí)行的代碼//一般用來釋放資源System.out.println("finally");}} } package demo05; //здЖ?вхвьГ? public class AgeException extends Exception{public AgeException(String message) {super(message);}}equals() ? 重寫equals()方法 ?搜索字符串
?
package demo06;public class Demo03 {//equals()方法public static void main(String[] args) {/*String s1 = new String("hello");String s2 = new String("hello");System.out.println(s1==s2);//false --比較地址值System.out.println(s1.equals(s2));//true --比較對象的內(nèi)容是否相同*/Employee e1 = new Employee(1,"eric","男",24,20000,"前端開發(fā)部");Employee e2 = new Employee(1,"eric","男",24,20000,"前端開發(fā)部");System.out.println(e1==e2);//falseSystem.out.println(e1.equals(e2));//true} } package demo06;public class Demo04 {public static void main(String[] args) {String str = "java是一名面向?qū)ο蟮恼Z言,java語言基于面向?qū)ο笏枷?#34;+ "通過學習java語言,可以加深對面向?qū)ο笏枷氲睦斫?#34;; //搜索第一個java的位置int idx = str.indexOf("java",0);System.out.println(idx);//搜索第二個java的位置idx = str.indexOf("java",idx+1);System.out.println(idx);//搜索倒數(shù)第一個面向?qū)ο骾nt idx2 = str.lastIndexOf("面向?qū)ο?#34;);System.out.println(idx2);//搜索倒數(shù)第二個面向?qū)ο骾dx2 = str.lastIndexOf("面向?qū)ο?#34;, idx2-1);System.out.println(idx2);} } package demo06;import java.util.Arrays; import java.util.Scanner;public class Demo05 {/*** (將輸入的多個英文名轉(zhuǎn)換為"Tom"的形式)tom TOM toM ... */public static void main(String[] args) {Scanner sc = new Scanner(System.in);String[] arr = new String[3];for (int i = 0; i < 3; i++) {System.out.print("請輸入第"+(i+1)+"個英文名:");arr[i] = sc.nextLine();}System.out.println(Arrays.toString(arr));for (int i = 0; i < arr.length; i++) {String name = arr[i];//(name.charAt(0)+"").toUpperCase() 首字符大寫//name.substring(1).toLowerCase(); 其余字符小寫arr[i] = (name.charAt(0)+"").toUpperCase() +name.substring(1).toLowerCase();}System.out.println(Arrays.toString(arr));} } package demo06;public class Employee {private static final Object Employee = null;//私有的成員變量private int id;private String name;private String sex;private int age;private float salary;private String dept;//空參的構(gòu)造方法public Employee() {super();// TODO Auto-generated constructor stub}//有參的構(gòu)造方法public Employee(int id, String name, String sex, int age, float salary, String dept) {super();this.id = id;this.name = name;this.sex = sex;this.age = age;this.salary = salary;this.dept = dept;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public float getSalary() {return salary;}public void setSalary(float salary) {this.salary = salary;}public String getDept() {return dept;}public void setDept(String dept) {this.dept = dept;}//展示員工信息public void printInfo(){System.out.println(id+","+name+","+sex+","+age+","+salary+","+dept);}//加薪public void addSalary(float salary){this.salary += salary;}//重寫equals()方法public boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Employee other = (Employee) obj;if (age != other.age)return false;if (dept == null) {if (other.dept != null)return false;} else if (!dept.equals(other.dept))return false;if (id != other.id)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (Float.floatToIntBits(salary) != Float.floatToIntBits(other.salary))return false;if (sex == null) {if (other.sex != null)return false;} else if (!sex.equals(other.sex))return false;return true;}/*@Overridepublic boolean equals(Object obj) {if(obj==null){return false;}if(obj==this){return true;}if(!(obj instanceof Employee)){return false;}Employee other = (Employee)obj;return this.id==other.id && this.name.equals(other.name)&& this.sex.equals(other.sex) && this.age==other.age && this.salary==other.salary && this.dept.equals(other.dept);}*/}?
包裝類型(統(tǒng)計字符數(shù)量)/ hashCode() ? StringBuffer/ pingSQL/打印時間/2018年美式日歷
? 包裝類型(統(tǒng)計字符數(shù)量)?
package demo06;public class Demo01 {/*** 包裝類型*/public static void main(String[] args) {//構(gòu)造方法創(chuàng)建對象Integer i = new Integer("10");System.out.println(i);//可以接受空值i = null;//包裝類型和基本類型之間可以自由轉(zhuǎn)換 //自動裝箱Integer a = 20;// new Integer(20);//自動拆箱int b = new Integer(30);//可以將字符串解析成數(shù)值類型int num = Integer.parseInt("666");System.out.println(num);Integer num2 = Integer.valueOf("888");System.out.println(num2);} } package demo06;import java.util.Arrays; import java.util.Scanner;public class Demo02 {/*** 案例:從控制臺輸入一個字符串"hello123ABC##@@"統(tǒng)計由多少個大寫字母,小寫字母,數(shù)字,其他字符*/public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入一個字符串:");String str = sc.nextLine();int[] counts = new int[4];//將字符串str拆成一個字符數(shù)組char[] chs = str.toCharArray();//System.out.println(Arrays.toString(chs));for(char c:chs){if(Character.isUpperCase(c)){//判斷是否為大寫字母counts[0]++;}else if(Character.isLowerCase(c)){//小寫字母counts[1]++;}else if(Character.isDigit(c)){//數(shù)字counts[2]++;}else{//其他字符counts[3]++;}}System.out.println("大寫字母:"+counts[0]);System.out.println("小寫字母:"+counts[1]);System.out.println("數(shù)字:"+counts[2]);System.out.println("其他字符:"+counts[3]);} }? ? ? ?hashCode() ? StringBuffer
package demo06;public class Demo06 {public static void main(String[] args) {/*String s = "hello";System.out.println(s.hashCode());s += "world";System.out.println(s.hashCode());*/StringBuffer s = new StringBuffer("hello");System.out.println(s.hashCode());System.out.println(s);s.append("world");System.out.println(s);System.out.println(s.hashCode());} } package demo06;import java.util.Scanner;public class Demo07 {/*** 從控制臺輸入一個字符串* 判斷它是否是回文* 上海自來水來自海上* @param args*/public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入一個回文:");String str = sc.nextLine();StringBuffer s = new StringBuffer(str);//反過來再轉(zhuǎn)回字符串String str2 = s.reverse().toString();if(str.equals(str2)){System.out.println("是回文");}else{System.out.println("不是回文");}} }?pingSQL
package demo06;import java.util.Scanner;public class Demo08 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入姓名:");String name = sc.nextLine();System.out.print("請輸入年齡:");String age = sc.nextLine();String sql = pingSQL(name,age);System.out.println(sql);}private static String pingSQL(String name, String age) {StringBuffer s = new StringBuffer("select * from emp where 1=1");if(name!=null && !"".equals(name)){s.append(" and name like '%"+name+"%'");}if(age!=null && !"".equals(age)){s.append(" and age="+age);}return s.toString();} }打印時間
package demo06;import java.text.SimpleDateFormat; import java.util.Date;public class Demo09 {public static void main(String[] args) {Date d = new Date();//Tue Jul 17 16:37:01 CST 2018//2018年7月17日 16時37分xx秒System.out.println(d);SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a HH時mm分ss秒");String dateStr = sdf.format(d);System.out.println(dateStr);} } package demo06;import java.util.Calendar;public class Demo10 {public static void main(String[] args) {//創(chuàng)建一個日歷對象Calendar cal = Calendar.getInstance();int year = cal.get(Calendar.YEAR);System.out.println(year);int month= cal.get(Calendar.MONTH);//月:0~11 1月~12月System.out.println(month);int date = cal.get(Calendar.DATE);//1~31System.out.println(date);//將2018年修改為2016年cal.set(Calendar.YEAR, 2016);//時間偏移方法//7月-->5月cal.add(Calendar.MONTH,-2);//17日-->20日cal.add(Calendar.DATE, 3);} }打印本月日歷
package demo06;import java.util.Calendar;public class Demo11 {/*** 打印本月日歷* @param args*/public static void main(String[] args) {Calendar cal = Calendar.getInstance();//將時間移動到本月的第一天cal.set(Calendar.DAY_OF_MONTH, 1);//本月第一天在所在周是第幾天int start = cal.get(Calendar.DAY_OF_WEEK);//打印日歷頭System.out.println("日\t一\t二\t三\t四\t五\t六");//System.out.println(start);for (int i = 1; i <=7*6; i++) {if(i<start){System.out.print("\t");continue;}//打印日歷System.out.print(cal.get(Calendar.DATE)+"\t");//打印7個空一行if(i%7==0){System.out.println();}//判斷是否已經(jīng)到了本月的最后一天if(cal.get(Calendar.DATE) == cal.getActualMaximum(Calendar.DAY_OF_MONTH)){break;}//日期的值增加1天cal.add(Calendar.DATE, 1);}} }2018年美式日歷
package demo06;import java.util.Calendar;public class Demo12 {//打印2018年全年的日歷public static void main(String[] args) {Calendar cal = Calendar.getInstance();cal.set(Calendar.YEAR, 2018);System.out.println("\t\t\t2018年日歷\t\t\t");for (int i = 0; i < 12; i++) {System.out.println("*************"+(i+1)+"月份***************");cal.set(Calendar.MONTH,i );generateCal(cal);}}private static void generateCal(Calendar cal) {//將時間移動到本月的第一天cal.set(Calendar.DAY_OF_MONTH, 1);//本月第一天在所在周是第幾天int start = cal.get(Calendar.DAY_OF_WEEK);//打印日歷頭System.out.println("日\t一\t二\t三\t四\t五\t六");//System.out.println(start);for (int i = 1; i <=7*6; i++) {if(i<start){System.out.print("\t");continue;}//打印日歷System.out.print(cal.get(Calendar.DATE)+"\t");//打印7個空一行if(i%7==0){System.out.println();}//判斷是否已經(jīng)到了本月的最后一天if(cal.get(Calendar.DATE) == cal.getActualMaximum(Calendar.DAY_OF_MONTH)){break;}//日期的值增加1天cal.add(Calendar.DATE, 1);}System.out.println();} }?
?
總結(jié)
以上是生活随笔為你收集整理的0717 抽象类/接口/异常/==-equals()/搜索字符串/包装类型(统计字符数)/hashCode()/StringBuffer/pingSQL/2018年美式日历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c 连接mysql批量存储数据库_C语言
- 下一篇: 女儿我的礼物