java新手笔记9

1.bank类

package com.yfs.javase;
//类封装
public class BankCard {

	//属性
	int balance;//默认0  实例变量 分配给每个对象一个
	//String name;

	// 方法 存钱
	public void saveMoney(int   money) {
		if(money < 0) {
			System.out.println("非法操作...");
			return;
		}
		System.out.println("向账户存入" + money +"元...");
		balance += money;
	}

	//取钱  调用时传入参数
	public void drawMoney(int money) {
		//验证余额
		if(balance < money) {
			System.out.println("账户余额不足...");
			return; //终止方法调用
		}

		 System.out.println("从账户取出" + money + "元...");
		 balance -= money;
	}

}

2.测试

package com.yfs.javase;

public class CardTest {

	public static void main(String[] args) {
//		  BankCard bc1 = null;//声明引用
//		  bc1 = new BankCard();//创建对象
		  BankCard bc1 = new BankCard();
		  System.out.println("银行卡余额:" + bc1.balance);
		  bc1.drawMoney(200);//调用对象方法

		  bc1.saveMoney(5000);

		  bc1.drawMoney(500000);

		  bc1.drawMoney(500);

		  bc1.saveMoney(-500);
		  System.out.println("银行卡余额:" + bc1.balance);

	}

}

3.测试1

package com.yfs.javase;

public class CardTest2 {

	public static void main(String[] args) {

		  BankCard bc1 = new BankCard();
		  System.out.println("银行卡余额:" + bc1.balance);
		  bc1.saveMoney(5000);
		  bc1.drawMoney(500);
		  System.out.println("银行卡余额:" + bc1.balance);

		  BankCard bc2 = new BankCard();
		  System.out.println("银行卡余额:" + bc2.balance);
		  bc2.saveMoney(3000);
		  System.out.println("bc2银行卡余额:" + bc2.balance);
		  System.out.println("bc1银行卡余额:" + bc1.balance);

	}

}

4.测试2

package com.yfs.javase;

public class CardTest3 {

	public static void main(String[] args) {

		  BankCard bc1 = new BankCard();
		  bc1.saveMoney(5000);
		  bc1.drawMoney(500);
		  System.out.println("银行卡余额:" + bc1.balance);

		  BankCard bc2 = new BankCard();
		  bc2.saveMoney(3000);
		  System.out.println("bc2银行卡余额:" + bc2.balance);
		  bc2.drawMoney(5000);
		  bc2.balance = 10000;//修改余额  不安全操作
		  bc2.drawMoney(5000);
		  System.out.println("bc2银行卡余额:" + bc2.balance);

	}

}

1.1bank类

package com.yfs.javase.update;
//类封装
public class BankCard {

	//属性  private 属性私有  只能在当前类中访问
	private int balance;// 实例变量 分配给每个对象一个
	private int password;

	// 方法 存钱
	public void saveMoney(int   money) {
		if(money < 0) {
			System.out.println("非法操作...");
			return;
		}
		System.out.println("向账户存入" + money +"元...");
		balance += money;//在方法中访问
	}

	//取钱  调用时传入参数
	public void drawMoney(int money) {
		//验证余额
		if(balance < money) {
			System.out.println("账户余额不足...");
			return; //终止方法调用
		}

		 System.out.println("从账户取出" + money + "元...");
		 balance -= money;
	}
	//通过访问balance属性方法 不能修改
	public void  getBalance () {
		System.out.println("账户余额:" + balance);
	}

}

2.1测试

package com.yfs.javase.update;

public class CardTest3 {

	public static void main(String[] args) {

		  BankCard bc1 = new BankCard();
		  bc1.saveMoney(5000);
		  bc1.drawMoney(50000);
//		  bc1.balance = 100000000;//编译出错
//		  System.out.println("银行卡余额:" + bc1.balance);//私有属性不能访问

		  bc1.getBalance();
		 // bc1.password;

	}

}

3.1方法重载条件

package com.yfs.javase.update;

import java.util.Random;

public class Demo2 {
//	public int add(int a, int b) {
//		return a + b;
//	}
    //变量名称不能成为重载条件
//	public int add(int c , int d) {
//		return c + d;
//	}
	public double add(int a , double b) {

		return a + b;

	}
	//返回值类型不能成为重载条件
	public long add( int a, int b) {

		return a + b;
	}
	public static void main(String[] args) {
		Demo2 d1 = new Demo2();
		long val = d1.add(1,6);

	}

}

4.1方法重载示例

package com.yfs.javase.update;

public class OverRideDemo {
	//方法的重载  方法使用相同的名字
	public int add (int a, int b) {
		 return a + b;
	}

	public  int  add (int a , int b, int c) {
		return a + b + c;
	}

	public double add( double a, double b) {
		return a + b;
	}
	public static void main(String[] args) {
		//创建对象
		OverRideDemo od = new OverRideDemo();
		int val = od.add(3,5);
		System.out.println("add(3,5) = " + val);

		val = od.add(3,5,8);//根据调用时传入参数个数 类型匹配对应方法
		System.out.println("add1(3,5,8) = " + val);

		double result = od.add(3.14, 2.71);
		System.out.println("add(3.14, 2.71) = " + result);

	}

}
时间: 2024-10-06 17:56:53

java新手笔记9的相关文章

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

1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = false; //跳出方法 同步锁 public synchronized void jump() { while(true) { if(!isLive ) { System.out.println("跳出第 " + index + " 田鼠"); //修改田鼠状态

java新手笔记34 连接数据库

1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtil { private static final String driver = "sun.j

java新手笔记10 构造器

1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java.util.Random; public class Demo1 { /** * 模拟摇奖 */ public static void main(String[] args) { Random ran = new Random(); int[] a = new int[7]; System.out.p

java新手笔记15 多态

1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println("动物叫..."); } } 2.Dog/Cat/Bird类 package com.yfs.javase; public class Dog extends Animal { public void cry() { System.out.println("汪 汪..."); }

java新手笔记1

//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { System.out.println("Hello World!"); } } //编译命令 C:\>javac C:\Users\Administrator\Desktop\Hello.java //javac 路径+文件名.java //javac -d D:\ C:\Users\

java新手笔记16 面积

1.图形类 package com.yfs.javase; public class Shape { //计算面积方法 public double getArea() { System.out.println("计算面积"); return 0; } } 2.圆 package com.yfs.javase; public class Circle extends Shape { private double r; public Circle(double r) { this.r =

java新手笔记2

1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void main(String[] args) { System.out.println("World World!"); //System.out.println("welcome java world!"); //注释的代码不执行 单行注释 /* 多行注释 System.

java新手笔记3

1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.println("a = " + a); //a = -a; //+ - System.out.println("a = " + a); //+ 字符串链接 System.out.println("影分身" + "软件开发"); Sys

java新手笔记23 异常

1.import package com.yfs.javase; import java.util.Scanner; //import java.lang.String;//默认导入 public class Demo1 { public static void main(String[] args) { String s = new String("abc");//java.lang.String String s1 = "abc"; System.out.pri