JAva初心者ノート13継承

4184 ワード

1、Person類
package com.yfs.javase;



//       

public class Person {



	private String name;//         

	private int age;

	private char sex;



	private void privateMehtod() {//            

		System.out.println("call privateMehtod()...");

	}



	public Person() {

		// privateMehtod();

		System.out.println("  Perosn  ...");

	}



	public Person(String name) {

		this.name = name;

	}



	public Person(String name, int age, char sex) {

		this.name = name;

		this.age = age;

		this.sex = sex;

	}



	public void introduce() {

		System.out.println("I am Person....");

	}



	public String toString() {

		return "  :" + name + "     :" + age + "    :" + sex;

	}



	public void speak() {

		System.out.println(name + "     ?");

		// privateMehtod();

	}



	public void sleep() {

		System.out.println(name + "     ?");

	}



	public void eat() {

		System.out.println(name + "    ?");

	}



	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;

	}



	public char getSex() {

		return sex;

	}



	public void setSex(char sex) {

		this.sex = sex;

	}



}


 2.Studioクラス
package com.yfs.javase;



//           

public class Student extends Person {//   Person



	private int score;



	@Override

	public void introduce() {

		System.out.println("I am student...");

	}

	//           

	@Override

	public String toString() {

		return super.toString() + "    :" + score;

	}



	public int getScore() {

		return score;

	}



	public void setScore(int score) {

		this.score = score;

	}



	public Student() {//             

		// super();//           super()       

		// super("Jack");

		System.out.println("  Student  ...");

	}



	//     

	public void study() {

		// getName()    

		// privateMethod();        

		// super.toString();//      

		// this super

		System.out.println(getName() + "    ...");//         

	}



}



//         

class YfsStudent extends Student {



}


 3.Teacherクラス
package com.yfs.javase;



public class Teacher extends Person {



	@Override

	public void introduce(/*int a*/) {

		System.out.println("I am teacher...");

	}

}


4.Personテスト
package com.yfs.javase;



public class PersonTest {



	public static void main(String[] args) {

		Person p1 = new Person();

		p1.setName("  ");

		p1.setAge(20);

		p1.setSex(' ');

		System.out.println(p1);//  toString  

		

		Student s1 = new Student();

		s1.setName("  ");

		s1.setAge(22);

		s1.setSex(' ');

		System.out.println(s1);

		

		Teacher t1 = new Teacher();

		t1.setName("  ");

		t1.setAge(30);

		t1.setSex(' ');

		System.out.println(t1);

		

		YfsStudent ys = new YfsStudent();

		ys.setName("Tom");

		System.out.println(ys);

		//ys.

		

		s1.study();

		//p1.study();//          

		ys.study();



	}



}


 5.Studioテスト
package com.yfs.javase;



public class StudentTest {



	public static void main(String[] args) {

		//        

		//Student s1 = new Student("zhangsan",20,' ');

		

		//Student s2 = new Student("lisi");

		

		Student s3 = new Student();//        

		s3.setName("  ");

		s3.speak();

		System.out.println(s3);

		s3.eat();

		

		Teacher t1 = new Teacher();

		t1.setName("  ");

		t1.eat();

		System.out.println(t1);

		//Person p1 = new Person("  ",23,' ');



	}



}


 6.Teacherクラステスト
package com.yfs.javase;



public class TeacherTest {



	public static void main(String[] args) {

		Person p1 = new Person();

		p1.setName("  ");

		

		Student s1 = new Student();

		s1.setName("  ");

		

		Teacher t1 = new Teacher();

		t1.setName("  ");

		

		p1.introduce();

		s1.introduce();

		t1.introduce();



	}



}