期末预习之java进阶篇(一)

一、类与对象

1.1 用类制造对象

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.2 定义类

例1.2 商品售卖机

package ch2;

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

在这里插入图片描述
在这里插入图片描述
类是定义了这个类的所有对象长什么样,而对象是这个类的一个个具体实例

1.3 成员变量和成员函数

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
成员变量与本地变量
在这里插入图片描述

1.4 对象初始化

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、对象交互

2.1 对象的识别

在这里插入图片描述

package clock;

public class Display {
	private int value = 0;
	private int limit = 0;
	
	public Display(int limit) {
		this.limit = limit;
	}
	
	public void increase()
	{
		value++;
		if(value ==limit)
			value = 0;
	}
	
	public int getValue()
	{
		return value;
	}
	public static void main(String[] args) {
		Display d =new Display(24);
		for(; ;) {
			d.increase();
			System.out.println(d.getValue());
		}
	}
}
2.2 对象交互

在这里插入图片描述
clock是老大!指挥hour和minute
在这里插入图片描述
在这里插入图片描述

package clock;

public class Clock {
	private Display hour = new Display(24);
	private Display minute = new Display(60);
	
	public void start() {
		while(true) {
		minute.increase();
		if(minute.getValue()==0)
		{
			hour.increase();
		}
		System.out.printf("%02d:%02d\n", hour.getValue(),minute.getValue());
	}
	}
	public static void main(String[] args)
	{
		Clock clock = new Clock();
		clock.start();
	}
}
2.3 封闭的访问属性

在这里插入图片描述
能够使用成员变量的地方只有两个:

  1. 在成员函数里面(可能是构造函数,可能是普通函数)
  2. 定义初始化的地方可以使用已经定义好的成员变量
    在这里插入图片描述
    在这里插入图片描述
2.4开放的访问属性

在这里插入图片描述
在这里插入图片描述
.java表示一个编译单元。
import java.util.Scanner;
每一个.都代表一个层次

2.5 类变量(static)
package clock;

public class Display {
	private int value = 0;
	private int limit = 0;
	public static int steep = 0;
	
	public Display(int limit) {
		this.limit = limit;
	}
	
	public void increase()
	{
		value++;
		if(value ==limit)
			value = 0;
	}
	
	 int getValue()
	{
		return value;
	}
	public static void main(String[] args) {
		Display d1 = new Display(10);
		Display d2 = new Display(20);
		d1.increase();
		System.out.println(d1.getValue());
		System.out.println(d2.getValue());
		System.out.println(d1.steep);
		System.out.println(d2.steep);
		d1.steep = 2;
		System.out.println(d1.steep);
		System.out.println(d2.steep);
		}
	}

输出情况
在这里插入图片描述
(static)类变量它是这个类的变量,它不属于它的任何一个对象,它属于这个类,任何一个对象都拥有它,这个变量不在每个对象里。
在这里插入图片描述

2.6 类函数

static的类和变量不属于任何具体的对象。

package clock;

public class Display {
	private int value = 0;
	private int limit = 0;
	public static int steep = 0;
	
	public Display(int limit) {
		this.limit = limit;
	}
	
	public void increase()
	{
		value++;
		if(value ==limit)
			value = 0;
	}
	
	 int getValue()
	{
		return value;
	}
	public static void main(String[] args) {
		Display d1 = new Display(10);
		d1.steep = 2;
		System.out.println(d1.steep);
		Display d2 = new Display(20);
		System.out.println(d2.steep);

		}
	}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值