abstract class A{
private String name = "WWW" ;
public void fun(){
// 此处定义了一个普通方法
System.out.println("Hello World!!!") ;
}
public abstract void print() ;
}; |
abstract class A{
private String name = "WWW" ;
public void fun(){
// 此处定义了一个普通方法
System.out.println("Hello World!!!") ;
}
public abstract void print() ;
};
public class Demo01{
public static void main(String args[]){
A a = new A() ;
}
}; |
abstract class A{
private String name = "WWW" ;
public void fun(){
// 此处定义了一个普通方法
System.out.println("Hello World!!!") ;
}
public abstract void print() ;
};
class B extends A{
// 覆写抽象方法
public void print(){
System.out.println("**********************") ;
}
};
public class Demo02{
public static void main(String args[]){
B b = new B() ;
b.print() ;
b.fun() ;
}
}; |
abstract class Person{
private String name ;
private int age ;
// 单独按一个类来说,应该可以直接在构造方法中初始化
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public abstract String getInfo() ;
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
};
class Student extends Person{
private String school ;
public Student(String name,int age,String school){
super(name,age) ;
this.school = school ;
}
public String getInfo(){
return "姓名:"+super.getName()+",年龄:"+super.getAge()+",学校:"+this.school ;
}
};
public class Demo03{
public static void main(String args[]){
Student stu = new Student("张三",30,"北京大学") ;
System.out.println(stu.getInfo()) ;
}
}; |
3.2、接口
interface A{
public static final String INFO = "HELLO" ;
public abstract void print() ;
} |
interface A{
public static final String INFO = "HELLO" ;
public abstract void print() ;
}
class B implements A{
public void print(){
System.out.println(INFO) ;
}
};
public class Demo04{
public static void main(String args[]){
B b = new B() ;
b.print() ;
}
}; |
interface A{
public static final String INFO = "HELLO" ;
public abstract void print() ;
}
interface C{
public static final String PARAM = "WWW" ;
public abstract void fun() ;
}
class B implements A,C{
public void print(){
System.out.println(INFO) ;
}
public void fun(){
System.out.println(PARAM) ;
}
};
public class Demo05{
public static void main(String args[]){
B b = new B() ;
b.print() ;
b.fun() ;
}
}; |
interface A{
// public static final String INFO = "HELLO" ;
String INFO = "HELLO" ;
// public abstract void print() ;
void print() ; //à 此处虽然没有明确的写出权限,但实际上权限是public
} |
interface A{
// public static final String INFO = "HELLO" ;
String INFO = "HELLO" ;
// public abstract void print() ;
void print() ;
}
abstract class X{
public String param = "hello" ;
abstract void fun() ;
};
class B extends X implements A{
public void print(){
System.out.println(INFO) ;
}
void fun(){
System.out.println(INFO) ;
System.out.println(param) ;
}
};
public class Demo07{
public static void main(String args[]){
B b = new B() ;
b.print() ;
}
}; |
interface A{
// public static final String INFO = "HELLO" ;
String INFO = "HELLO" ;
// public abstract void print() ;
void print() ;
}
abstract class X implements A{
public String param = "hello" ;
abstract void fun() ;
}; |
interface A{
public void printA() ;
}
interface B{
public void printB() ;
}
interface C{
public void printC() ;
}
interface X extends A,B,C{
public void printX() ;
};
class Y implements X{
public void printA(){}
public void printB(){}
public void printC(){}
public void printX() ;
}; |