l protected class A protected int n 5

  • Slides: 32
Download presentation

l 继承过程中,protected的访问权限不变 class A{ protected int n = 5; protected int increase. Num(int a){

l 继承过程中,protected的访问权限不变 class A{ protected int n = 5; protected int increase. Num(int a){ return a+1; } } A B C class E{ …… D object 1 = new D(); object 1. firstnum = 10; object 1. n = 50; } D 要求E类和D类 在同一个包中 高级语言程序设计(Java) http: //www. swust. net. cn 要求E类和A类 在同一个包中

abstract class Person{ private String; public Person(String n){ name = n; 对于abstract方法,只 } 允许声明,不允许实现

abstract class Person{ private String; public Person(String n){ name = n; 对于abstract方法,只 } 允许声明,不允许实现 public abstract String get. Description(); public String get. Name(){ return name; } } 高级语言程序设计(Java) http: //www. swust. net. cn

class Student extends Person{ private String major; pulbic Student(String n, String m){ super(n); 抽象类的子类必须实现

class Student extends Person{ private String major; pulbic Student(String n, String m){ super(n); 抽象类的子类必须实现 major = m; abstract方法,即重写 父类的abstract方法。 } public String get. Description(){ return “a student majoring” + major; } } 高级语言程序设计(Java) http: //www. swust. net. cn

class FIFOQueue implements Collection { interface Collection void add ( Object obj ) {

class FIFOQueue implements Collection { interface Collection void add ( Object obj ) { { …… } int MAX_NUM=100; void delete( Object obj ) void add (Object obj); { …… void delete (Object obj); } Object find( Object obj ) Object find (Object obj); { …… int current. Count ( ); } int current. Count() } { …… } } 高级语言程序设计(Java) http: //www. swust. net. cn

class A{ private double y=12. 56; class B { int y=0; public void g(){

class A{ private double y=12. 56; class B { int y=0; public void g(){ y=y+100; System. out. printf("y=%dn", y); } } } 高级语言程序设计(Java) http: //www. swust. net. cn

l 匿名类的主要用途就是向方法的参数传值。 this. addwindow. Listener(new Window. Ada. Pter(){ publie void window. Closing(Window. Event e){

l 匿名类的主要用途就是向方法的参数传值。 this. addwindow. Listener(new Window. Ada. Pter(){ publie void window. Closing(Window. Event e){ System. exit(0); } }); 高级语言程序设计(Java) http: //www. swust. net. cn

public class Exception. Example{ public static void main(String[ ] args){ int n=0, m=0, t=0;

public class Exception. Example{ public static void main(String[ ] args){ int n=0, m=0, t=0; try{ t=9999; 不能转换成数 m=Integer. parse. Int("8888"); 字,出现异常 n=Integer. parse. Int("12 s 3 a"); System. out. println("我没有机会输出"); }catch(Exception e){ System. out. println("发生异常"); catch捕获并处 理try语句块里 n=123; 面的异常 } System. out. println("n="+n+", m="+m+", t="+t); } } 高级语言程序设计(Java) http: //www. swust. net. cn

class Simple. Exception extends Exception public class My. Exception. Demo { { public void

class Simple. Exception extends Exception public class My. Exception. Demo { { public void f() throws Simple. Exception { public My. Exception() { System. out. println("Throw Simple. Exception from f()"); } throw new Simple. Exception(); } } public static void main(String[] args) { Simple. Exception. Demo sed = new Simple. Exception. Demo(); 运行结果: try { sed. f(); Throw Simple. Exception from } catch(Simple. Exception e) { f() System. out. println(e); Simple. Exception System. out. println("Caught it!"); Caught it! } } } 高级语言程序设计(Java) http: //www. swust. net. cn

class Chorus<E, F>{ void make. Chorus(E person, F yueqi){ person. to. String(); yueqi. to.

class Chorus<E, F>{ void make. Chorus(E person, F yueqi){ person. to. String(); yueqi. to. String(); } } 高级语言程序设计(Java) http: //www. swust. net. cn