6_Java_类与对象

对象是实体,需要被创建;
类是规范,根据类的定义来创建对象;
对象 = 属性 + 服务

把数据和对数据的操作放在一起:封装;

定义类

一个类由两种东西组成:表示对象有什么的成员变量和表示对象能做什么的成员函数。

一旦定义了类,我们就可以创建这个类的多个对象,这些对象都会做那个类所定义的动作(函数),但是各自具有不同的数据。

对象变量是对象的管理者;

类名称 对象变量名称 = new 类名称();

写在类里的成员变量,只是一个声明,变量并不在那里,变量不在类里,变量在每一个对象里。

package first;

public class First {
    
    
	int price = 80;
	int balance;
	int total;
	void show()
	{
    
    
		System.out.println("Welcome!!!");
	}
	void insertMoney(int amount)
	{
    
    
		balance += amount;
	}
	void showBalance()
	{
    
    
		System.out.println(balance);
	}
	void getFood()
	{
    
    
		if(balance >= price)
		{
    
    
			System.out.println("Here you are");
			balance -= price;
			total += price;
		}
	}
	public static void main(String[] args) {
    
    
		First vm = new First();
		vm.show();
		vm.showBalance();
		vm.insertMoney(100);
		vm.getFood();
		vm.showBalance();
		First vm1 = new First();
		vm1.insertMoney(200);
		vm.showBalance();
		vm1.showBalance();
	}

}
成员变量和成员函数
  • 类定义了对象中所具有的变量,这些变量称作成员变量;
  • 每个对象有自己的变量,和同一个类的其他对象是分开的;
  • 在函数中可以直接写成员变量的名字来访问成员变量;
  • 函数是通过对象来调用的:这次调用零时建立了 insertMoney() 和 vm 之间的关系,insertMoney() 内部的成员变量指的是 vm 的成员变量;
this

this 是成员函数的一个特殊的固有的本地变量,它表达了调用这个函数的那个对象;

package first;

public class First {
    
    
	int price = 80;
	int balance;
	int total;
	void setPrice(int price)
	{
    
    
		this.price = price;
	}
	void show()
	{
    
    
		System.out.println("Welcome!!!");
	}
	void insertMoney(int amount)
	{
    
    
		balance += amount;
	}
	void showBalance()
	{
    
    
		System.out.println(this.balance);
	}
	void getFood()
	{
    
    
		if(balance >= price)
		{
    
    
			System.out.println("Here you are");
			balance -= price;
			total += price;
		}
	}
	public static void main(String[] args) {
    
    
		First vm = new First();
		vm.show();
		vm.setPrice(88);
		vm.showBalance();
		vm.insertMoney(100);
		vm.getFood();
		vm.showBalance();
		First vm1 = new First();
		vm1.insertMoney(200);
		vm1.showBalance();
	}

}
调用函数
  • 通过 . 运算符调用某个对象的函数;
  • 在成员函数内部直接调用自己(this)的其他函数;
注意
  • 定义在函数内部的变量是本地变量;
  • 本地变量的生存期和作用域都是函数内部;
  • 成员变量的生存期是对象的生存期,作用域是类内部的成员函数;
对象的初始化

Java提供了多种手段来保障对象创建时的初始化,包括给每个成员变量默认的“0”值、定义初始化和构造函数。

构造函数

如果有一个成员函数的名字和类的名字完全相同,则在创建这个类的每一个对象的时候会自动调用这个函数,构造函数;

构造函数不能有返回类型;

package first;

public class First {
    
    
	int price = 80;
	int balance;
	int total = 20;
	First()
	{
    
    
		total = 10;
	}
	First(int price)
	{
    
    
		this.price = price;
	}
	void setPrice(int price)
	{
    
    
		this.price = price;
	}
	void show()
	{
    
    
		System.out.println("Welcome!!!");
	}
	void insertMoney(int amount)
	{
    
    
		balance += amount;
	}
	void showBalance()
	{
    
    
		System.out.println(this.balance);
	}
	void getFood()
	{
    
    
		if(balance >= price)
		{
    
    
			System.out.println("Here you are");
			balance -= price;
			total += price;
		}
	}
	public static void main(String[] args) {
    
    
		First vm = new First();
		vm.show();
		vm.setPrice(88);
		vm.showBalance();
		vm.insertMoney(100);
		vm.getFood();
		vm.showBalance();
		First vm1 = new First(22);
		vm1.insertMoney(200);
		vm1.showBalance();
	}

}
函数重载
  • 一个类可以有多个构造函数,只要它们的参数表不同;
  • 创建对象的时候给出不同的参数值,就会自动调用不同的构造函数;
  • 通过this() 还可以调用其他的构造函数;
  • 一个类里的同名但参数表不同的函数构成了重载关系;
package first;

public class First {
    
    
	int price = 80;
	int balance;
	int total = 20;
	First()
	{
    
    
		total = 10;
	}
	First(int price)
	{
    
    
		this(); //调用其他构造函数
		this.price = price;
	}
	void setPrice(int price)
	{
    
    
		this.price = price;
	}
	void show()
	{
    
    
		System.out.println("Welcome!!!");
	}
	void insertMoney(int amount)
	{
    
    
		balance += amount;
	}
	void showBalance()
	{
    
    
		System.out.println(this.balance);
	}
	void getFood()
	{
    
    
		if(balance >= price)
		{
    
    
			System.out.println("Here you are");
			balance -= price;
			total += price;
		}
	}
	public static void main(String[] args) {
    
    
		First vm = new First();
		vm.show();
		vm.setPrice(88);
		vm.showBalance();
		vm.insertMoney(100);
		vm.getFood();
		vm.showBalance();
		First vm1 = new First(22);
		vm1.insertMoney(200);
		vm1.showBalance();
	}

}
package first;

public class First {
    
    
	int price = 80;
	int balance;
	int total = 20;
	First()
	{
    
    
		this(32); //调用其他构造函数
		total = 10;
	}
	First(int price)
	{
    
    
		this.price = price;
	}
	void setPrice(int price)
	{
    
    
		this.price = price;
	}
	void show()
	{
    
    
		System.out.println("Welcome!!!");
	}
	void insertMoney(int amount)
	{
    
    
		balance += amount;
	}
	void showBalance()
	{
    
    
		System.out.println(this.balance);
	}
	void getFood()
	{
    
    
		if(balance >= price)
		{
    
    
			System.out.println("Here you are");
			balance -= price;
			total += price;
		}
	}
	public static void main(String[] args) {
    
    
		First vm = new First();
		vm.show();
		vm.setPrice(88);
		vm.showBalance();
		vm.insertMoney(100);
		vm.getFood();
		vm.showBalance();
		First vm1 = new First(22);
		vm1.insertMoney(200);
		vm1.showBalance();
	}

}
函数重载与类型自动转换

有两个函数重载,一个是double,一个是int,它们是两个不同的函数,系统会根据参数类型来调用特定的构造函数;

package first;

public class First {
    
    
	int price = 80;
	int balance;
	int total = 20;
	First()
	{
    
    
		this(32); //调用其他构造函数
		total = 10;
	}
	First(int price)
	{
    
    
		this.price = price;
	}
	First(double price)
	{
    
    
		this.price = (int)price;
	}
	void setPrice(int price)
	{
    
    
		this.price = price;
	}
	void show()
	{
    
    
		System.out.println("Welcome!!!");
	}
	void insertMoney(int amount)
	{
    
    
		balance += amount;
	}
	void showBalance()
	{
    
    
		System.out.println(this.balance);
	}
	void getFood()
	{
    
    
		if(balance >= price)
		{
    
    
			System.out.println("Here you are");
			balance -= price;
			total += price;
		}
	}
	public static void main(String[] args) {
    
    
		First vm = new First(22.33);
		vm.show();
		vm.setPrice(88);
		vm.showBalance();
		vm.insertMoney(100);
		vm.getFood();
		vm.showBalance();
		First vm1 = new First(22);
		vm1.insertMoney(200);
		vm1.showBalance();
	}

}

package hello;

import java.util.Scanner;


public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		Fraction a = new Fraction(in.nextInt(), in.nextInt());
		Fraction b = new Fraction(in.nextInt(), in.nextInt());
		a.print();
		b.print();
		a.plus(b).print();
		a.multiply(b).plus(new Fraction(5,6)).print();
		a.print();
		b.print();
		in.close();
	}
}
class Fraction {
    
    
	int x;
	int y;
	Fraction()
	{
    
    
		
	}
	Fraction(int x, int y)
	{
    
    
		this.x = x;
		this.y = y;
	}
	void print()
	{
    
    
		int a, b, r;
		a = x;
		b = y;
		r = a % b;
		if(a == b)
		{
    
    
			System.out.println(1);
			return;
		}
		while(r != 0)
		{
    
    
			a = b;
			b = r;
			r = a % b;
		}
		System.out.println(x / b + "/" + (y / b));
	}
	double toDouble()
	{
    
    
		return x/y;
	}
	Fraction plus(Fraction r)
	{
    
    
		Fraction result = new Fraction();
		result.x = this.x * r.y + r.x * this.y;
		result.y = this.y * r.y;
		return result;
	}
	Fraction multiply(Fraction r)
	{
    
    
		Fraction result = new Fraction();
		result.x = this.x * r.x;
		result.y = this.y * r.y;
		return result;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45459526/article/details/122646251