package com.itguigu.java;
public class AnimalTest {
public static void main(String[] args) {
Animal a = new Animal();
a.name = "大黃";
a.show();
a.setLegs(10);
a.show();
System.out.println(a.getLegs());
}
}
class Animal{
String name;
private int age;
private int legs;
public void setLegs(int l) {
if(l>=0 && l%2==0) {
legs = l;
}else {
legs = 0;
}
}
public int getLegs() {
return legs;
}
public void eat() {
System.out.println("動物進食");
}
public int getAge() {
return age;
}
public void setAge(int a) {
age = a;
}
public void show() {
System.out.println(name + "," + age + "," + legs);
}
}
package com.itguigu.exer;
public class Person {
private int age;
public void setAge(int a) {
if(a<0 || a>130) {
System.out.println("傳入的資料非法");
}else {
age = a;
}
}
public int getAge() {
return age;
}
}
package com.itguigu.exer;
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setAge(12);
System.out.println(p1.getAge());
}
}
package com.itguigu.java;
public class PersonTest {
public static void main(String[] args) {
Person p = new Person();
p.eat();
Person p1 = new Person("Tom");
System.out.println(p1.name);
}
}
class Person{
String name;
int age;
public Person() {
System.out.println("Person()...");
}
public Person(String n) {
name = n;
}
public Person(String n, int a) {
name = n;
age = a;
}
public void eat() {
System.out.println("人吃飯");
}
public void study() {
System.out.println("人可以學習");
}
}
package com.itguigu.exer;
public class Triangle {
private double base;
private double height;
public Triangle() {
}
public Triangle(double b, double h) {
base = b;
height = h;
}
public void setBase(double b) {
base = b;
}
public double getBase() {
return base;
}
public void setHeight(double h) {
height = h;
}
public double getHeight() {
return height;
}
}
package com.itguigu.exer;
public class TriangleTest {
public static void main(String[] args) {
Triangle t1 = new Triangle();
t1.setBase(2.0);
t1.setHeight(2.4);
System.out.println(t1.getBase() + "," + t1.getHeight());
Triangle t2 = new Triangle(5.1, 5.2);
System.out.println(t1.getBase() + "," + t1.getHeight());
}
}
package com.itguigu.java;
public class UserTest {
public static void main(String[] args) {
User u = new User();
System.out.println(u.age);
User u1 = new User(2);
u1.setAge(3);
System.out.println(u1.age);
}
}
class User{
String name;
int age=1;
public User() {
}
public User(int a) {
age = a;
}
public void setAge(int a) {
age = a;
}
}
package com.itguigu.java;
public class Customer {
private int id;
private String name;
public Customer() {
}
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
package com.itguigu.java1;
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setAge(1);
System.out.println(p1.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
}
public Person(String name) {
this();
this.name = name;
}
public Person(int age) {
this();
this.age = age;
}
public Person(String name, int age) {
this(age);
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
package com.itguigu.exer;
public class boy {
private String name;
private int age;
public boy() {
}
public boy(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void marry(girl girl) {
System.out.println(girl.getName());
}
public void shout() {
if(this.age>=22) {
System.out.println("你可以去合法等級結婚類!");
}else {
System.out.println("先多談談戀愛");
}
}
}
package com.itguigu.exer;
public class girl {
private String name;
private int age;
public girl() {
}
public girl(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void marry(boy boy) {
System.out.println(boy.getName());
boy.marry(this);
}
public int compare(girl girl) {
if(this.age > girl.age) {
return 1;
}else if(this.age < girl.age) {
return -1;
}else {
return 0;
}
}
}
package com.itguigu.exer;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id, double balance, double annualInterestRate) {
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 void withdraw(double amount) {
if(balance < amount) {
System.out.println("餘額不足");
return;
}
balance -= amount;
System.out.println("成功取出" + amount);
}
public void deposit(double amount) {
if(amount > 0) {
balance += amount;
System.out.println("成功存:" + amount);
}
}
}
package com.itguigu.exer;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
package com.itguigu.exer1;
public class Account {
private double balance;
public Account(double init_balance) {
this.balance = init_balance;
}
public double getBalance() {
return this.balance;
}
public void deposit(double amt) {
if(amt > 0) {
balance += amt;
System.out.println("存錢成功");
}
}
public void withdraw(double amt) {
if(balance >= amt) {
balance -= amt;
System.out.println("取錢成功");
}else {
System.out.println("餘額不足");
}
}
}
package com.itguigu.exer1;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
package com.itguigu.exer1;
public class Bank {
private Customer[] customers;
private int numberOfCustomers;
public Bank() {
customers = new Customer[10];
}
public void addCustomer(String f, String l) {
Customer cust = new Customer(f, l);
customers[numberOfCustomers++] = cust;
}
public int getNumberOfCustomers() {
return this.numberOfCustomers;
}
public Customer getCustomer(int index) {
if(index >=0 && index < numberOfCustomers) {
return customers[index];
}else {
return null;
}
}
}
package com.itguigu.java1;
import java.util.Arrays;
public class PackageImportTest {
public static void main(String[] args) {
System.out.println(Arrays.toString(new int[] {1,2,3}));
}
}