Java_学习笔记_003(面向对象案例练习)

简单Java类

class Address {
    private String country ;
    private String province ;
    private String city ;
    private String street ;
    private String zipcode ;

    public Address(){}
    public Address(String country,String province,String city,String street,String zipcode){
        this.country = country;
        this.province = province;
        this.city = city;
        this.street =street;
        this.zipcode = zipcode;
    }

    public String showInf(){
        return "国家" + this.country + "\n省份:" + this.province +"\n城市:" + this.city + "\n街道:" + this.street + "\n邮编:" + this.zipcode;
    }

    public void setCountry(String country) {
        this.country = country;
    }
    public void setProvince(String province) {
        this.province = province ;
    }
    public void setCity(String city) {
        this.city = city ;
    }
    public void setStreet(String street) {
        this.street = street ;
    }
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode ;
    }

    public String getCity() {
        return city;
    }

    public String getCountry() {
        return country;
    }

    public String getProvince() {
        return province;
    }

    public String getStreet() {
        return street;
    }

    public String getZipcode() {
        return zipcode;
    }

}

public class JavaCase1 {
    public static void main(String[] args) {
        Address addr = new Address("中国","四川","雅安","新康路","625000");
        System.out.println(addr.showInf());
    }
}

 /*
    定义并测试一个代表员工的employee类
    员工属性包括:编号、姓名、基本工资、工资增长率
    包括计算薪水增长额度以及增长后的工资总额度操作的方法
 */

/*
    定义并测试一个代表员工的employee类
    员工属性包括:编号、姓名、基本工资、工资增长率
    包括计算薪水增长额度以及增长后的工资总额度操作的方法
 */
class Employee{
    private long empno;
    private String ename;
    private double salary;
    private double rate;
    // setter、getter 省略
    public Employee(){}
    public Employee(long empno,String ename,double salary,double rate){
        this.empno = empno;
        this.ename = ename;
        this.salary = salary;
        this.rate = rate;
    }
    public String showInf(){
        return "员工编号:" + this.empno + "\n姓名:" + this.ename + "\n工资:" + this.salary + "\n增长率:"  + this.rate ;
    }
    public double salaryInc(){
        return this.salary * this.rate;
    }
    public double salaryIncResult(){
        this.salary = this.salary * (1 + this.rate);
        return this.salary;
    }
}

public class JavaCase2 {
    public static void main(String[] args) {
        Employee emp = new Employee(03555L,"张成",4500.00,0.2);
        System.out.println(emp.showInf());
        System.out.println(emp.salaryInc());
        System.out.println(emp.salaryIncResult());
    }

}

 

/*

    设计一个Dog类.
    属性有名字、颜色、年龄,定义构造方法来初始化这些属性,定义showInf()方法输出dog信息
 */

/*\
    设计一个Dog类.
    属性有名字、颜色、年龄,定义构造方法来初始化这些属性,定义showInf()方法输出dog信息
 */
class Dog{
    private String dname;
    private String dcolor;
    private int dage;

    public Dog(){}
    public Dog(String dname,String dcolor,int dage){
        this.dname = dname;
        this.dcolor = dcolor;
        this.dage = dage;
    }

    public String showInf(){
        return "名字:" + this.dname + "\n颜色:" + this.dcolor + "\n年龄:" + this.dage;
    }
}

public class JavaCase3 {
    public static void main(String[] args) {
        Dog dog = new Dog("乖乖","蓝色",1);
        System.out.println(dog.showInf());
    }
}

/*
    构造一个银行账户类,包含以下内容:
    数据成员有 账户名称、账户余额
    方法包括   开户(设置账户名称以及余额),利用构造方法完成、查询余额
 */

/*
    构造一个银行账户类,包含以下内容:
    数据成员有 账户名称、账户余额
    方法包括   开户(设置账户名称以及余额),利用构造方法完成、查询余额
 */
class Account{
    private String name;
    private double balance;

    public Account(){}
    public Account(String name){
        this(name,0.0);
    }
    public Account(String name,double balance){
        this.name = name;
        this.balance = balance;
    }

    // seeter、getter 省略
    public String getInf(){
        return "账户名称:" + this.name + "\n账户余额:" + this.balance;
    }

    public double getBalance() {
        return balance;
    }

}

public class JavaCase4 {
    public static void main(String[] args) {
        Account account = new Account("TYW",5201314.00);
        System.out.println(account.getInf());
    }
}

 

/*
    设计一个表示用户的User类,类中属性有用户名、口令和记录用户个数的变量
    定义3个类的构造方法(无参数,为用户名赋值,为用户名和口令赋值)
    设置口令的方法、获取口令的方法、返回类的信息方法
 */

/*
    设计一个表示用户的User类,类中属性有用户名、口令和记录用户个数的变量
    定义3个类的构造方法(无参数,为用户名赋值,为用户名和口令赋值)
    设置口令的方法、获取口令的方法、返回类的信息方法
 */
class User{
    private String username;
    private String password;
    private static int userNumber;

    public User(){
        this("none","none");
    }
    public User(String username){
        this(username,"none");
    }
    public User(String username,String password){
        this.username = username;
        this.password = password;
        userNumber++;
    }
    // seeter getter 省略

    public String getInfo(){
        return "用户名:" + this.username + "\n密码:" + this.password;
    }

    public static String getCountNumber(){
        return "用户数量:" + userNumber;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }
}

public class JavaCase5 {
    public static void main(String[] args) {
        User user = new User("administrator","admin");
        User user1 = new User("helloworld");

        System.out.println(user.getInfo());

        System.out.println(user1.getInfo());

        System.out.println(User.getCountNumber());
    }
}


/*
    申明一个图书类,数据成员为:
    书名、编号(利用静态变量实现自动编号)、书价、并拥有静态数据成员 册数、记录图书册数总量
    在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义多个对象,并求出总册数
 */

/*
    申明一个图书类,数据成员为:
    书名、编号(利用静态变量实现自动编号)、书价、并拥有静态数据成员 册数、记录图书册数总量
    在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义多个对象,并求出总册数
 */
class Book{
    private String bname;
    private int bid;
    private double bprice;
    private static int bnumber = 1;

    public Book(){
        this("none",0.0);
    }
    public Book(String bname){
        this(bname,0.0);
    }
    public Book(String bname,double bprice){
        this.bid = bnumber++;
        this.bprice = bprice;
        this.bname = bname;
    }

    public String getInfo(){
        return "编号:" + this.bid + "\n书名:" + this.bname + "\n价格:" + this.bprice;
    }
    // seeter、getter 省略
    public static String getBnumber() {
        return "总册数:" + (bnumber-1);
    }
}

public class JavaCase6 {
    public static void main(String[] args) {
        Book book = new Book();
        Book book1 = new Book("C++");
        Book book2 = new Book("Java",25.00);
        Book book3 = new Book("database",30.00);

        System.out.println(book.getInfo());
        System.out.println(book1.getInfo());
        System.out.println(book2.getInfo());
        System.out.println(book3.getInfo());

        System.out.println(Book.getBnumber());


    }
}

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页