分装

编程入门 行业动态 更新时间:2024-10-15 08:27:16

分装

分装

笔记:  1.面向对象的特征一:封装与隐藏问题:当创建了类的对象以后,如果直接通过“对象.属性”的方式对相应的对象属性赋值的话,可能会出现不满足实际情况的意外,我们考虑不让对象来直接作用于属性,而是通过“对象.方法”的形式,来控制对象对属性的访问。实际情况中,对属性的要求就可以通过方法来体现
解决的方法:(封装性的思想)将类的属性私有化,提供公共的方法(setter & getter)来实现调用
2.权限修饰符:从大到小排列:public protected 缺省 private
注:1)四种权限都可以用来修饰属性、方法、构造器
 2)修饰类的权限有:public 缺省

package com.beicai.oop;

public class Person {
private String name;
private int age;
private String school;
private String major;

public void setName(String i){
if(i != null){
name = i;
}else{
System.out.println("你的输入错误");
}
}
public String getName(){
return name;
}
public void setAge(int a){
if(a >0){
age=a;
}else{
System.out.println("您输入错误");
}
}
public int getAge(){
return age;
}
public void setSchool(String s){
if(s != null){
school= s;
}else{
System.out.println("你的输入错误");
}
}
public String getSchool(){
return school;
}
public void setmajor(String m){
if(m!=null){
major =m;
}
}
public String getmajor(){
return major;
}
}




package com.beicai.oop;

public class Testperson {
public static void main(String[] args) {
 Person p =new  Person();
 p.setName("老朱");
 System.out.println(p.getName());
 p.setSchool("北财");
System.out.println(p.getSchool());
p.setAge(19);
System.out.println(p.getAge());
p.setmajor("");
System.out.println(p.getmajor());

}
}



package com.beicai.oop;

public class TriAngle {
private int bast;
private int height;

public void setBast(int a){
if(a >0){
bast = a;
}else{
System.out.println("输入错误");
}
}
public int getBast(){
return bast;
}
public void setheight(int b){
if(b>0){
height =b;
}else{
System.out.println("输出错误");
}
}
public int getheight(){
return height;
}

}



package com.beicai.oop;

public class TestTriAngle {

public static void main(String[] args) {
TriAngle t =new TriAngle();
t.setBast(5);
t.setheight(4);
System.out.println(t.getBast()*t.getheight()/2);
}

}


package constructor.test;


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 void withdraw1(int i) {
// TODO Auto-generated method stub

}
public void deposit1(int i) {
// TODO Auto-generated method stub

}
public void deposit(int i) {
// TODO Auto-generated method stub

}
public void withdraw11(int i) {
// TODO Auto-generated method stub

}
public void withdraw(int i) {
// TODO Auto-generated method stub

}


}










package constructor.test;


public class Customer {


private String firstName;
private String lastName;
private Account account;
public Customer(String firstName, String lastName, Account account) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.account = account;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}




}








package constructor.test;


public class TestCustomer {
public static void main(String[] args) {
Customer cust = new Customer("Jane","Smith", null );
cust.setAccount(new Account(1000,2000,0.0123));
Account account =cust.getAccount();
account.deposit(100);
account.withdraw(960);
account.withdraw(2000);
System.out.println("Customer ["+cust.getLastName()+","+cust.getFirstName()
+","+cust.getFirstName()+"]has a account;id is "+account.getId()+",annualInterestRate is"
+account.getAnnualInterestRate()*100+"%,balance is"+account.getBalance());
}
}

更多推荐

分装

本文发布于:2024-03-06 08:18:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1714828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!