08-01

package com.atguigu.exer;

public class ManKind {

  private int sex;

  private int salary;

  public ManKind() {

  }

  public ManKind(int sex, int salary) {

     this.sex = sex;

     this.salary = salary;

  }

  public void manOrWoman() {

     if (sex == 1) {

       System.out.println("man");

     } else if (sex == 0) {

       System.out.println("woman");

     }

  }

  public void employeed() {

     if (salary == 0) {

       System.out.println("no job");

     } else {

       System.out.println("job");

     }

  }

  public int getSex() {

     return sex;

  }

  public void setSex(int sex) {

     this.sex = sex;

  }

  public int getSalary() {

     return salary;

  }

  public void setSalary(int salary) {

     this.salary = salary;

  }

}

package com.atguigu.exer;

public class Kids extends ManKind {

  private int yearsOld;

  public Kids() {

  }

  public Kids(int yearsOld) {

     this.yearsOld = yearsOld;

  }

  public void printAge() {

     System.out.println("I am " + yearsOld + " years old");

  }

  public int getYearsOld() {

     return yearsOld;

  }

  public void setYearsOld(int yearsOld) {

     this.yearsOld = yearsOld;

  }

}

package com.atguigu.exer;

public class KidsTest {

  public static void main(String[] args) {

     Kids someKid = new Kids(12);

     someKid.printAge();

     someKid.setSex(1);

     someKid.setSalary(0);

     someKid.employeed();

     someKid.manOrWoman();

  }

}

package com.atguigu.exer1;

public class Circle {

  private double redius;

  public Circle() {

     redius = 1.0;

  }

  public double getRedius() {

     return redius;

  }

  public void setRedius(double redius) {

     this.redius = redius;

  }

  public double findArea() {

     return Math.PI * redius * redius;

  }

}

package com.atguigu.exer1;

public class Cylinder extends Circle {

  private double length;

  public Cylinder() {

     length = 1.0;

  }

  public double getLength() {

     return length;

  }

  public void setLength(double length) {

     this.length = length;

  }

  public double findVolume() {

     return findArea() * getLength();

  }

}

package com.atguigu.exer1;

public class CylinderTest {

  public static void main(String[] args) {

     Cylinder cy = new Cylinder();

     cy.setRedius(2.1);

     cy.setLength(3.4);

     double volume = cy.findVolume();

     System.out.println("体积为" + volume);

     double area = cy.findArea();

     System.out.println("圆面积" + area);

  }

}

package com.atguigu.java2;

public class Person {

  String name;

  int age;

  int id = 1001;

  public Person() {

  }

  public Person(String name) {

     this.name = name;

  }

  public Person(String name, int age) {

     this(name);

     this.age = age;

  }

  public void eat() {

     System.out.println("人,吃饭");

  }

  public void walk() {

     System.out.println("人:吃饭");

  }

}

package com.atguigu.java2;

public class Student extends Person {

  String major;

  int id = 1002;

  public Student() {

  }

  public Student(String major) {

     this.major = major;

  }

  public Student(String name, int age, String major) {

     super(name, age);

     this.major = major;

  }

  @Override

  public void eat() {

     System.out.println("学生:多吃有营养的食物");

  }

  public void study() {

     this.eat();

     super.eat();

  }

  // public void study(){

  // System.out.println("学生:学习知识");

  // }

  public void show() {

     System.out.println("name =" + name + "age=" + age);

     System.out.println("id = " + id);

     System.out.println("id = " + super.id);

  }

}

package com.atguigu.java2;

/*

 * super关键字的使用

 * 1super理解为:父类的

 * 2super可以用来调用:属性、方法、构造器

 * 3super的使用

 *     3.1我们可以在子类的方法或构造器中。通过使用“super.属性super.方法的方式,显示的调用

 *     父类中声明的属性或方法。但是,通常情况下,我们习惯省略super

 *    

 *     3.2特殊情况:当子类和父类中定义了同名的属性时,我们想要在子类中调取父类中声明的属性,

 *     则必须显示的使用“super.属性的方式,表明调用的是父类中声明的属性。

 *

 *     3.3特殊情况:当子类重写了父类中发方法一后,我们想在子类的方法中调用子类被重写的方法时,

 *     则必须使用显示的“super.方法的方式,表明调用的是父类中被重写的方法

 *  

 *   4super调用构造器

 *     4.1我们可以在子类的构造器中显式的使用“super(形参列表)的方式,调用父类中声明的指定构造器

 *     4.2“super(形参列表)的使用,必须声明在子类构造器的首行

 *     4.3我们在类的构造器中,针对“this(形参列表)“super(形参列表)只能二选一

 *     4.4在构造器的首行,没有显示的声明“this(形参列表)“super(形参列表)则默认调用的是父类中空参的构造器

 *     4.5在类的多个构造器中,至少有一个类的构造器中使用super(形参列表),调用父类中的构造器

 *

 */

public class SuperTest {

  public static void main(String[] args) {

     Student s1 = new Student();

     s1.show();

     s1.study();

     Student s2 = new Student("tom", 22, "it");

     s1.show();

  }

}

package com.atguigu.java3;

public class Person {

  String name;

  int age;

  public void eat() {

     System.out.println("人,吃饭");

  }

  public void walk() {

     System.out.println("人:走路");

  }

}

package com.atguigu.java3;

public class Man extends Person {

  boolean isSomking;

  public void earnMoney() {

     System.out.println("男人挣钱");

  }

  public void eat() {

     System.out.println("男人多吃肉,张肌肉");

  }

  public void walk() {

     System.out.println("男人霸气的走路");

  }

}

package com.atguigu.java3;

public class Woman extends Person {

  boolean isBeauty;

  public void goShopping() {

     System.out.println("女人喜欢购物");

  }

  public void eat() {

     System.out.println("女人少吃,为了减肥");

  }

  public void walk() {

     System.out.println("女人走路窈窕");

  }

}

package com.atguigu.java3;

/*

 * 面向对象特征之三:多态性

 *

 * 1、理解多态性:可以理解为一个事务的多种形态

 * 2、何为多态性

 *     对象的多态性:子类的对象赋给父类的引用

 * 3、多态的使用:虚拟方法调用

 *     有了对象的多态性以后,我们在编译期,只能调用父类声明的方法,但在运行期,

 *     我们实际上执行的是子类重写父类的方法

 *

 * 4、多态性的使用前提 类的继承关系  ②方法的重写

 *

 * 5、对象的多态性,只适用于方法,不适用于属性

 *

 */

public class PersonTest {

  public static void main(String[] args) {

     Person p1 = new Person();

     p1.eat();

     Man man = new Man();

     man.eat();

     man.age = 25;

     man.earnMoney();

     // ********************************************************

     // 对象多态性:父类的引用指向子类的对象

     Person p2 = new Man();

     p2.eat();

  }

}

package com.atguigu.java3;

public class AnimalTest {

  public static void main(String[] args) {

     AnimalTest test = new AnimalTest();

     test.func(new Dog());

     test.func(new Cat());

  }

  public void func(Animal animal) {

     animal.eat();

     animal.shout();

  }

}

class Animal {

  public void eat() {

     System.out.println("动物:吃");

  }

  public void shout() {

     System.out.println("动物:叫");

  }

}

class Dog extends Animal {

  public void eat() {

     System.out.println("狗吃骨头");

  }

  public void shout() {

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

  }

}

class Cat extends Animal {

  public void eat() {

     System.out.println("猫吃鱼");

  }

  public void shout() {

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

  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值