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.Student类

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.Student测试

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(); } }

java新手笔记13 继承的更多相关文章

  1. JAVA自学笔记13

    JAVA自学笔记13 1.StringBuffer类 1)线程安全的可变字符序列 线程安全(即同步) 2)StringBuffer与String的区别:一个可变一个不可变 3)构造方法: ①publi ...

  2. Java基础笔记-抽象,继承,多态

    抽象类: abstract修饰 抽象方法必须定义在抽象类中,抽象类不能创建对象. 在抽象方法中可以不定义抽象方法,作用是:让该类不能建立对象. 特点是: 1.定义在抽象类中 2.方法和类都用abstr ...

  3. 1.8(java学习笔记)继承与方法的重写

    继承 在java中可以通过继承提高代码的复用率. 例如A继承了B,就可以是 例如,首先有一个类似Person,这个类中有有一些属性和方法,我们再新建一个Student类,其中有一部分属性和方法与Per ...

  4. Java 学习笔记(6)——继承

    之前说过了Java中面向对象的第一个特征--封装,这篇来讲它的第二个特征--继承.一般在程序设计中,继承是为了减少重复代码. 继承的基本介绍 public class Child extends Pa ...

  5. java新手笔记14 类继承示例

    1.Person package com.yfs.javase; public class Person { private String name; private int age; private ...

  6. Java学习笔记之继承

    一.继承的基础 在Java术语中,被继承的类叫超类(superclass)或者父类,继承超类的类叫子类(subclass). 举例说明: class Box { public double width ...

  7. java新手笔记33 多线程、客户端、服务器

    1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = ...

  8. java新手笔记32 jdk5新特性

    1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...

  9. java新手笔记20 抽象类模板(letter)

    1.抽象类 package com.yfs.javase; //信模板 public abstract class Templater { public abstract String toName( ...

随机推荐

  1. (转载)链表环中的入口点 编程之美 leecode 学习

    http://www.cnblogs.com/hiddenfox/p/3408931.html 说的很细 /** * Definition for singly-linked list. * clas ...

  2. Dynamic Vertex Buffers

    ynamic vertex buffers on the other hand allow us to manipulate the information inside the vertex buf ...

  3. 各大算法专题-STL篇

    这篇文章着重记录c++中STL的用法.主要粗略的介绍其用法,以知识点的形式呈现其功能,不会深入源码分析其工作原理. 排序和检索. sort(a,a+n),对a[0]往后的n个元素(包括a[0])进行排 ...

  4. 《征服c指针》学习笔记-----统计文本单词数目的程序word_count

    1.程序的要求:对用户指定的英文文本文件(包括标准输入),将英文单词按照字母顺序输出到用户指定的文本文件中(包括标准输出),并且在各单词后面显示单词的出现次数. 2.模块设计: 主要分为:1.从输入流 ...

  5. centos 7 安装mp3解码器

    1. Install the nux repo  $> su - $> yum update # optional but recommanded $> rpm --import h ...

  6. A Tour of Go Type conversions

    The expression T(v) converts the value v to the type T. Some numeric conversions: var i int = 42 var ...

  7. mongodb的查询操作符

    本文地址:http://www.cnblogs.com/egger/archive/2013/05/04/3059374.html   欢迎转载 ,请保留此链接! 官方参考: http://docs. ...

  8. JBPM学习(一):实现一个简单的工作流例子全过程

    test.png test.jpdl.xml <?xml version="1.0" encoding="UTF-8"?> <process ...

  9. python学习(4)

    python(4)4.1 高阶函数:map/reduce map:实际上map也是一个函数,只不过他可以在参数里面包含别的函数.他有两种参数:第一种:函数(作用在后面要说的序列上),第二种:一个序列  ...

  10. javascript Deferred和递归次数限制

    function runAsyncTTS(text,speecher,audiopath) { var def = jQuery.Deferred(); var args = {"Synth ...