多态
静态代码块
当类被载入是,静态代码块优先于静态方法被执行,且执行一次,静态代码块经常用来做初始化。
静态代码块的工作顺序
例子
class Person extends Object
{
static String name;
int num;
Person(){
name="yang";
num=22;
System.out.println("生成Person类");
}
static{
System.out.println("Person类的静态代码块开始执行");
}
}
public class Java {
public static void main(String args[]){
Person a=new Person();
System.out.println("生成Java类");
}
static {
System.out.println("Java类的静态代码块开始执行");
}
}
对抽象方法的使用
抽象类中可以包含一般的属性和static属性
也可以包含抽象方法和一般的方法
abstract class Person{
String name;
int age;
abstract String briefInfo();
public String greeting(){
return "How are you?";
}
}
class Student extends Person{
Student(){
name="Tom";
age=20;
}
public String briefInfo(){
return "My name is "+name+" and I am "+age+" years old";
}
}
class Worker extends Person{
Worker(){
name="Bob";
age=30;
}
public String briefInfo(){
return name+"is my name "+ "and I am "+age+" years old";
}
}
public class Java{
public static void main(String args[]){
Student aa=new Student();
Worker bb=new Worker();
System.out.println(aa.briefInfo());
System.out.println(bb.briefInfo());
System.out.println(aa.greeting());
}
}
对于接口来讲、
仅包含抽象方法和静态的数据成员,声明的静态数据成员一定要先赋初值,不要讲静态方法实例化。
一个类只能继承一个父类,当你需要多继承时,就需要用接口 来继承多个接口。
interface Name{
String name="Tom";
public abstract String showName();
}
interface Age{
int age=20;
public abstract int showAge();
}
class Person implements Name,Age{
public String showName(){
return name;
}
public int showAge(){
return age;
}
}
public class Java{
public static void main(String args[]){
Person tom=new Person();
System.out.println("I am "+tom.showName()+" and I am "+tom.showAge()+" years old!");
}
}
//接口继承接口
interface A{
final int a=10;
public abstract int sayA();
}
interface B{
final int b=20;
public abstract int sayB();
}
interface C extends B,A{
final int c=50;
public abstract int sayC();
}
class People implements A,B,C{
public int sayA(){
return a;
}
public int sayB(){
return b;
}
public int sayC(){
return c;
}
}
public class Java{
public static void main(String args[]){
People zzy=new People();
System.out.println(zzy.sayA()+" "+zzy.sayB()+" "+zzy.sayC()+" ");
}
}
上转型对像 不用强制类型转化
下转型对象。 需要对象进行转发。
例子
class Father{
private String surname;
public String showSurname(){
return surname;
}
public String greet(){
return "Hello I am father" ;
}
}
class Son extends Father{
public String greet(){
return " Hello I am son";
}
}
public class Java{
public static void main(String args[]){
Father father=new Son(); //由子类对象生成父类对象,这是father只能使用,子类son中继承father中没有复写和复习的属性和方法
Son son=(Son)father;
System.out.println(father.greet());
System.out.println(son.greet());
}
}
Instanceof() 可以判断一个类是否实现了某个接口,也可以判断一个实例对象是否属于一个类。
子类所创建的对象都是父类的实例,但是父类说创建的对象不是子类的实例 有上图instance例子可知。
接口来进行实例化,来定义出统一的标准。
第七章 异常处理
接口来进行实例化,来定义出统一的标准。
第七章 异常处理
抛出异常的两种方式 :
1. 程序中抛出异常 抛出的实例异常。
2. 指定方法抛出异常
//指定方法抛出异常类,有时java给你的异常类的项不能满足要求,这样就需要自己定义异常类。
class DefaultException extends Exception //继承异常类
{
DefaultException(String msg){
super(msg); //调用父类的构造方法
}
}
public class Java{
public static void main(String args[]){
try{
throw new DefaultException("自定义异常类"); //抛出自定义的异常类
}
catch(Exception e){
System.out.println(e.toString());
}
finally{
System.out.println("程序运行结束");
}
}
}
关于关键字 throw和throws这两个关键字的理解。
程序中如果要人为的抛出异常时,需要用到throw这个关键字。
若是方法会抛出异常,这可将处理此异常的try-catch-finally
写在调用此方法的程序代码中。
第八章 包及访问权限
举个将程序打包和导入包的例子。
打包的主要好处是避免在合作开发的过程中 代码的重名 而引发的错误。
// 打包和导入包
package demo.java;
public class DefaultException extends Exception //继承异常类
{
public DefaultException(String msg){
super(msg); //调用父类的构造方法
}
}
//第二段程序
package demo.test;
import demo.java.DefaultException; //将不在一个包中的文件导入进去。
public class Main{
public static void main(String args[]){
try{
throw new DefaultException("自定义错误");
}
catch (Exception e){
System.out.println(e.toString());
}
finally{
System.out.println("how are you!");
}
}
}
Java程序的运用
File文件的使用
import java.io.*;
public class Main
{
public static void main(String args[]){
File f=new File("c:/1.txt");
if(f.exists()==false){
try{
f.createNewFile();
}
catch(Exception e){
System.out.println(e);
}
}
System.out.println(f.getName());
}
}
RandomAccessFile 类支持“随机访问”方式,可以跳转到文件的任意文字处读取数据
而且 RandomAccessFile 在数据等长记录格式文件的随机读取时有很大的优势。
例子
流类的操作:
1. 使用file流找到一个文件
2. 通过file类的对象去实例化字节流或字符流的子类。
3. 进行字节(字符)的读写操作
4. 关闭文件流。
字符和字节流的区别
一个字符等于两个字节
及字符流更适合读入 中文
字节流更适合读入 英文
// FileInputStream 和 FileOutputStream 是用byte[]数组来存取数据的
import java.io.*;
public class Main
{
public static void main(String args[]){
File file=new File("c:\\1.txt");
byte a[]="hello china".getBytes();
FileOutputStream out=null;
FileInputStream in=null;
try{
out=new FileOutputStream(file);
out.write(a);
out.close();
}
catch(Exception e){
System.out.println(e);
}
byte b[]=new byte [1024];
int all=0;
try{
in=new FileInputStream(file);
all=in.read(b);
in.close();
}
catch(Exception e){
System.out.println(e);
}
finally{
System.out.println(new String(b,0,all)); //这里需要将Byte的长度写上否则会出现下面情况
System.out.println(new String(b));
}
}
}
运行后产生这两种效果。
关于流的应用
避免了字符和字节频繁的进行相互的转化
package demo.test;
// FileInputStream 和 FileOutputStream 是用byte[]数组来存取数据的
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException{
BufferedReader cin=new BufferedReader(new nputStreamReader(System.in));
String ss=null;
ss=cin.readLine();
system.out.println(ss);
}
}