JAVA 内部类、外部类、继承、抽象类、接口实战

1、 Person、Student类定义在JavaOop类里面,属于内部类, private String school是private 级别,Student类的s实例可以直接引用 School属性
2、将Person、Student类定义在JavaOop类外面,属于外部类,private String school是private 级别,Student类不可以直接引用 School属性,必须s.getSchool()来调用

 

 

package IMF;

public class JavaOop {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Student s = new Student();
     s.setName("Spark");
     s.setAge(6);
     s.setSchool("Big Data");
  
        
  System.out.println("Name:"+ s.getName() +"   age" + s.getAge()+"   school "+ s.getSchool());
  
  s.sayHello();
  
  Cat cat = new Cat();
  cat.act();
  
  Dream bigData = new BigData();
  bigData.toDo();
  
 }


}


class BigData implements Dream{

 @Override
 public void toDo() {
  // TODO Auto-generated method stub
  System.out.println("Life is short ,you need spark!");
 }
 
}

interface Dream{
 public String ID ="001";
 public void toDo();
}

abstract class Animal{
 private String name;
    public abstract void act();
   
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
}


class Cat extends Animal{

 @Override
 public void act() {
  // TODO Auto-generated method stub
  System.out.println("cat doing");
 }
 
 
 
}

 


class Person {
 private String name;
 private int age;
 
 
 final void sayHello(){
  System.out.println("Person say HEllo");
 }
 
 
 public Person () {
  System.out.println("Person's constructor!");
 }
 
 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;
 }
 
}
class Student extends Person {
 private String school;
 
 /*void sayHello(){
  super.sayHello();
  System.out.println("Student say HEllo");
 }*/
 
 public String getSchool() {
  return school;
 }

 public void setSchool(String school) {
  this.school = school;
 }

 public Student () {
  System.out.println("Student constructor!");
 }
 
 
}
运行结果如下:

Person's constructor!
Student constructor!
Name:Spark   age6   school Big Data
Person say HEllo
cat doing
Life is short ,you need spark!

你可能感兴趣的:(JAVA 内部类、外部类、继承、抽象类、接口实战)