Java通俗易懂30天 第12天: 面向对象进阶1

全部源码:https://github.com/name365/JavaSE-30Day

第四章 面向对象(中)

继承性的使用与理解

  • Person 类

public class Person {

	String name;
	private int age;

	public Person(){

	}

	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}

	public void eat(){
		System.out.println("吃饭");
		sleep();
	}

	private void sleep(){
		System.out.println("睡觉");
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
  • Student 类

public class Student extends Person {

	String major;

	public Student(){

	}

	public Student(String name,int age,String major){
		this.name = name;

		setAge(age);
		this.major = major;
	}

	public void study(){
		System.out.println("学习");
	}

	public void show(){
		System.out.println("name:" + name + ",age = " + getAge());
	}

}
  • 测试类

public class ExtendsTest {

	public static void main(String[] args) {
		Person p1 = new Person();

		p1.eat();
		System.out.println("********************");

		Student s1 = new Student();
		s1.eat();

		s1.name = "Tom";

		s1.setAge(10);
		System.out.println(s1.getAge());

	}
}


  • Java 中关于继承性的规定

public class ExtendsTest {

	public static void main(String[] args) {
		s1.brease();

		Creature c = new Creature();
		System.out.println(c.toString());

	}
}
  • 将上述 Person 类改为如下
public class Person extends Creature {
    ...

}
  • Creature 类
public class Creature {

	public void brease(){
		System.out.println("呼吸");
	}
}

继承性练习

  • 练习1

public class Kids extends ManKind{

	private int yearsOld;

	public Kids() {

	}

	public Kids(int yearsOld) {
		this.yearsOld = yearsOld;
	}

	public int getYearsOld() {
		return yearsOld;
	}

	public void setYearsOld(int yearsOld) {
		this.yearsOld = yearsOld;
	}

	public void printAge(){
		System.out.println("I am " + yearsOld);
	}
}
  • ManKind类

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 if(salary!=0){
			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;
	}

}
  • KidsTest

public class KidsTest {
	public static void main(String[] args) {

		Kids someKid = new Kids(12);

		someKid.printAge();

		someKid.setYearsOld(15);
		someKid.setSalary(0);
		someKid.setSex(1);

		someKid.employeed();
		someKid.manOrWoman();
	}
}
  • 练习2
public class Circle {

	public double radius;

	public Circle(){
		radius = 1.0;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public double findArea(){
		return Math.PI * radius * radius;
	}
}
  • Cylinder类
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() * length;
	}
}
  • 测试类
public class CylinderTest {
	public static void main(String[] args) {

		Cylinder cy = new Cylinder();

		cy.setRadius(2.1);
		cy.setLength(3.4);
		double volues = cy.findVolume();
		System.out.println("圆柱的体积:" + volues);

		double area = cy.findArea();
		System.out.println("圆的面积: " + area);
	}
}

方法的重写(override/overwrite)

  • Person类
public class Person {

	String name;
	int age;

	public Person(){

	}

	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}

	public void eat(){
		System.out.println("吃饭");
	}

	public void walk(int distance){
		System.out.println("走路,走的距离是:" + distance + "公里");
		show();
	}

	private void show(){
		System.out.println("我是一个人。");
	}

	public Object info(){
		return null;
	}

	public double info1(){
		return 1.0;
	}
}
  • Student类
public class Student extends Person{

	String major;

	public Student(){

	}

	public Student(String major){
		this.major = major;
	}

	public void study(){
		System.out.println("学习,专业是:" + major);
	}

	public void eat(){
		System.out.println("学生应该多吃有营养的。");
	}

	public void show(){
		System.out.println("我是一个学生。");
	}

	public String info(){
		return null;
	}

	@Override
	public void walk(int distance) {
		System.out.println("自动生成");
	}
}
  • 测试类

public class PersonTest {

	public static void main(String[] args) {
		Student s = new Student("计算机科学与技术");
		s.eat();
		s.walk(10);

		s.study();
	}
}

方法重写的细节

  • Person类
public class Person {

	String name;
	int age;

	public Person(){

	}

	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}

	static void eat(){
		System.out.println("吃饭");
	}

	public void walk(int distance){
		System.out.println("走路,走的距离是:" + distance + "公里");
		show();
	}

	private void show(){
		System.out.println("我是一个人。");
	}

	public Object info(){
		return null;
	}

	public double info1(){
		return 1.0;
	}
}
  • Student类
public class Student extends Person{

	String major;

	public Student(){

	}

	public Student(String major){
		this.major = major;
	}

	public void study(){
		System.out.println("学习,专业是:" + major);
	}

	public static void eat(){
		System.out.println("学生应该多吃有营养的。");
	}

	public void show(){
		System.out.println("我是一个学生。");
	}

	public String info(){
		return null;
	}

	@Override
	public void walk(int distance) {
		System.out.println("自动生成");
	}
}
  • 测试类

public class PersonTest {

	public static void main(String[] args) {
		Student s = new Student("计算机科学与技术");
		s.eat();
		s.walk(10);
		System.out.println("*******************");

		s.study();

		Person p1 = new Person();
		p1.eat();
	}
}

方法的练习

  • 练习1
1.如果现在父类的一个方法定义成private访问权限,在子类中将此方法声明为default访问权限,那么这样还叫重写吗?(NO)
  • 练习2

public class Kids extends ManKind{

	private int yearsOld;

	public Kids() {

	}

	public Kids(int yearsOld) {
		this.yearsOld = yearsOld;
	}

	public int getYearsOld() {
		return yearsOld;
	}

	public void setYearsOld(int yearsOld) {
		this.yearsOld = yearsOld;
	}

	public void printAge(){
		System.out.println("I am " + yearsOld);
	}

	public void employeed(){
		System.out.println("Kids should study and no job.");
	}
}
  • MindKids类
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 if(salary!=0){
			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;
	}

}
  • 测试类
public class KidsTest {
	public static void main(String[] args) {

		Kids someKid = new Kids(12);

		someKid.printAge();

		someKid.setYearsOld(15);
		someKid.setSalary(0);
		someKid.setSex(1);

		someKid.employeed();
		someKid.manOrWoman();
	}
}

四种访问权限修饰符

对于之前的四种权限修饰符的补充

  • Order类
package githubb;

public class Order {

	private int orderPrivate;
	int orderDefault;
	protected int orderProtected;
	public int orderPublic;

	private void methodPrivate(){
		orderPrivate = 1;
		orderDefault = 2;
		orderProtected = 3;
		orderPublic = 4;
	}

	void methodDefault(){
		orderPrivate = 1;
		orderDefault = 2;
		orderProtected = 3;
		orderPublic = 4;
	}

	protected void methodProtected(){
		orderPrivate = 1;
		orderDefault = 2;
		orderProtected = 3;
		orderPublic = 4;
	}

	public void methodPublic(){
		orderPrivate = 1;
		orderDefault = 2;
		orderProtected = 3;
		orderPublic = 4;
	}
}
  • Ordertest类
package githubb;

public class OrderTest {
	public static void main(String[] args) {

		Order order = new Order();

		order.orderDefault = 1;
		order.orderProtected = 2;
		order.orderPublic = 3;

		order.methodDefault();
		order.methodProtected();
		order.methodPublic();

	}
}

  • SubOrder类
package githubc;

import githubb.Order;

public class SubOrder extends Order{

	public void method(){
		orderProtected = 1;
		orderPublic = 2;

		methodProtected();
		methodPublic();

	}
}
  • OrderTest类
package githubc;

import githubb.Order;

public class OrderTest {
	public static void main(String[] args) {

		Order order = new Order();
		order.orderPublic = 1;
		order.methodPublic();

	}

	public void show(Order order){
		order.orderPublic = 1;
		order.methodPublic();

	}
}

关键字:super

  • Person类
public class Person {

	String name;
	int age;
	int id = 1003;

	public Person(){
		System.out.println("我无处不在");
	}

	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("人,走路");
	}
}
  • Student类
public class Student extends Person{

	String major;
	int id = 1002;

	public Student(){

	}

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

		super(name,age);
		this.major = major;
	}

	public Student(String major){
		this.major = major;
	}

	public void eat(){
		System.out.println("学生多吃有营养的食物");
	}

	public void Study(){
		System.out.println("学生,学习知识。");
		this.eat();

		super.eat();
		super.walk();
	}
	public void show(){
		System.out.println("name = " + this.name + ",age = " + super.age);
		System.out.println("id = " + this.id);
		System.out.println("id = " + super.id);
	}
}
  • 测试类

public class SuperTest {
	public static void main(String[] args) {

		Student s = new Student();
		s.show();

		s.Study();

		Student s1 = new Student("Ton",21,"IT" );
		s1.show();

		System.out.println("***********************");
		Student s2 = new Student();

	}
}

子类对象实例化过程



public class InstanceTest {

}

附件:实验继承-super.pdf

  • Account类

public class Account {

	private int id;
	private double balance;
	private double annualInterestRate;

	public Account(int id, double balance, double annualInterestRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}

	public double getMonthlyInterest(){
		return annualInterestRate / 12;
	}

	public void withdraw (double amount){
		if(balance >= amount){
			balance -= amount;
			return;
		}
		System.out.println("余额不足");
	}

	public void deposit (double amount){
		if(amount > 0){
			balance += amount;

		}
	}

}
  • AccountTest类

public class AccountTest {
	public static void main(String[] args) {
		Account acct = new Account(1122,20000,0.045);

		acct.withdraw(30000);
		System.out.println("你的账户余额为:" + acct.getBalance());
		acct.withdraw(2500);
		System.out.println("你的账户余额为:" + acct.getBalance());
		acct.deposit(3000);
		System.out.println("你的账户余额为:" + acct.getBalance());

		System.out.println("月利率为: " + (acct.getAnnualInterestRate() * 100) + "%");
	}
}
  • CheckAccount类

public class CheckAccount extends Account{

	private double overdraft;

	public CheckAccount(int id, double balance, double annualInterestRate,double overdraft){
		super(id, balance, annualInterestRate);
		this.overdraft = overdraft;
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

	@Override
	public void withdraw(double amount) {
		if(getBalance() >= amount){

			super.withdraw(amount);
		}else if(overdraft >= amount - getBalance()){

			overdraft -= (amount - getBalance());

			super.withdraw(getBalance());

		}else{
			System.out.println("超过可透支限额!");
		}

	}
}
  • CheckAccountTest类

public class CheckAccountTest {
	public static void main(String[] args) {
		CheckAccount cat = new CheckAccount(1122,20000,0.045,5000);

		cat.withdraw(5000);
		System.out.println("您的账户余额为: " + cat.getBalance());
		System.out.println("您的可透支额度为: " + cat.getOverdraft());

		cat.withdraw(18000);
		System.out.println("您的账户余额为: " + cat.getBalance());
		System.out.println("您的可透支额度为: " + cat.getOverdraft());

		cat.withdraw(3000);
		System.out.println("您的账户余额为: " + cat.getBalance());
		System.out.println("您的可透支额度为: " + cat.getOverdraft());
	}
}

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

  • Person类
public class Person {
	String name;
	int age;

	public void eat(){
		System.out.println("人,吃饭");
	}

	public void walk(){
		System.out.println("人,走路");
	}

}
  • Woman类
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("女人,窈窕的走路。");
	}
}
  • Man类
public class Man extends Person{

	boolean isSmoking;

	public void earnMoney(){
		System.out.println("男人负责工作养家");
	}

	public void eat() {
		System.out.println("男人多吃肉,长肌肉");
	}

	public void walk() {
		System.out.println("男人霸气的走路");
	}
}
  • 测试类

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();
	p2.walk();

	}
}
  • 举例

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

	public void func(Dog dog){
		dog.eat();
		dog.shout();
	}

	public void func(Cat cat){
		cat.eat();
		cat.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("喵!喵!喵!");
	}
}

虚拟方法的补充

import java.util.Random;

class Animal  {

	protected void eat() {
		System.out.println("animal eat food");
	}
}

class Cat  extends Animal  {

	protected void eat() {
		System.out.println("cat eat fish");
	}
}

class Dog  extends Animal  {

	public void eat() {
		System.out.println("Dog eat bone");

	}

}

class Sheep  extends Animal  {

	public void eat() {
		System.out.println("Sheep eat grass");

	}

}

public class InterviewTest {

	public static Animal  getInstance(int key) {
		switch (key) {
		case 0:
			return new Cat ();
		case 1:
			return new Dog ();
		default:
			return new Sheep ();
		}

	}

	public static void main(String[] args) {
		int key = new Random().nextInt(3);

		System.out.println(key);

		Animal  animal = getInstance(key);

		animal.eat();

	}

}

整个Java全栈系列都是笔者自己敲的笔记。写作不易,如果可以,点个赞呗!✌

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
面向对象是一种编程范式,它将数据和操作数据的方法打包在一起,形对象。在 Python 中,面向对象编程是一种非常常用的编程方式,可以帮助我们更好地组织代码和解决问题。下面是通俗易懂的理解和写好 Python 的面向对象方法: 1. 类和对象:面向对象编程的核心是类和对象。类是一种数据类型,它定义了一组属性和方法,可以用来创建对象。对象则是类的实例,它可以调用类的方法和访问类的属性。 2. 封装:封装是面向对象编程的一种重要概念,它可以将数据和方法封装在一起,形一个独立的单位。在 Python 中,可以使用类来实现封装。 3. 继承:继承是面向对象编程中的一种机制,它可以从已有的类中派生出新的类,新的类可以继承已有类的属性和方法。在 Python 中,可以使用继承来实现代码的重用。 4. 多态:多态是面向对象编程的另一个重要概念,它可以使不同的对象对相同的消息作出不同的响应。在 Python 中,可以通过方法重写和方法重载来实现多态。 在 Python 中,可以通过以下步骤来实现面向对象编程: 1. 定义类:使用 `class` 关键字定义一个类,然后定义类的属性和方法。 2. 创建对象:使用类名调用构造函数 `__init__` 来创建对象。 3. 访问属性和方法:使用点号 `.` 来访问对象的属性和方法,例如 `obj.attr` 和 `obj.method()`。 下面是一个简单的 Python 面向对象示例: ``` class Person: def __init__(self, name, age): self.name = name self.age = age def say_hello(self): print(f"Hello, my name is {self.name}, and I'm {self.age} years old.") person = Person("Alice", 30) person.say_hello() ``` 在上面的示例中,我们定义一个 `Person` 类,它有 `name` 和 `age` 两个属性,以及 `say_hello()` 方法。然后我们使用 `Person` 类创建了一个对象 `person`,并调用了 `say_hello()` 方法。 总之,面向对象编程是一种重要的编程方式,它可以帮助我们更好地组织代码和解决问题。在 Python 中,可以使用类和对象来实现面向对象编程,需要掌握类和对象的概念、封装、继承和多态等重要概念。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值