• 第5章 面向对象(基础篇)


       1、

    class Address {
        private String country;
        private String province;
        private String city;
        private String street;
        private String postcode;
        
        Address() {
            
        }
        
        Address(String country, String province, String city, String street, String postcode) {
            this.country = country;
            this.province = province;
            this.city = city;
            this.street = street;
            this.postcode = postcode;
        }
        
        public void show() {
            System.out.println("地址为:"+country+province+city+street+"    邮编: "+postcode);
        }
    }
    public class AddressTest {
    
        public static void main(String[] args) {
            new Address("中国", "湖北省", "天门市", "陆羽大道", "427806").show();
        }
    
    }

       2、

    class Employee {
        private String id;
        private String name;
        private double base_salary;
        private double increase_salary;
        
        Employee(String id, String name, double base_salary, double increase_salary) {
            this.id = id;
            this.name = name;
            this.base_salary = base_salary;
            this.increase_salary = increase_salary;
        }
        
        public double getIncrease_salary() {
            return increase_salary;
        }
        
        public double getTotalSalary() {
            return increase_salary + base_salary;
        }
        
    }
    public class EmployeeDemo {
    
        public static void main(String[] args) {
            Employee li = new Employee("211016040", "李阿昀", 5000, 2500.50);
            System.out.println("薪水增长额:"+li.getIncrease_salary());
            System.out.println("增长后的工资总额为:"+li.getTotalSalary());
        }
    
    }

       3、

    public class practice3 {
    
        public static void main(String[] args) {
            String str = "want you to know one thing";
            System.out.println("字母n出现的次数:"+getCharacterCounts(str, 'n'));
            System.out.println("字母o出现的次数:"+getCharacterCounts(str, 'o'));
        }
        
        public static int getCharacterCounts(String str, char ch) {
            int count = 0;
            
            for(int index = 0; (index = str.indexOf(ch, index)) != -1; index++) {
                count++;
            }
            return count;
        }
    
    }

       4、

    class Dog {
        private String name;
        private String color;
        private int age;
        public Dog(String name, String color, int age) {
            this.name = name;
            this.color = color;
            this.age = age;
        }
        
        public void show() {
            System.out.println("狗狗的名字是"+name+",颜色是"+color+",年龄是"+age+"岁。");
        }    
    }
    public class DogTest {
    
        public static void main(String[] args) {
            new Dog("旺财", "黄色", 5).show();
        }
    
    }

       5、

    class User {
        private String name;
        private String psw;
        private static int count = 0;
        
        User() {
            count++;
            System.out.println("创建第"+count+"个对象。");
        }
        User(String name) {
            this.name = name;
            count++;
            System.out.println("创建第"+count+"个对象。");
        }
        User(String name, String psw) {
            this.name = name;
            this.psw = psw;
            count++;
            System.out.println("创建第"+count+"个对象。");
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPsw() {
            return psw;
        }
        public void setPsw(String psw) {
            this.psw = psw;
        }
        
        public void show() {
            System.out.println("用户名:"+name+",口令:"+psw);
        }
    }
    public class UserTest {
    
        public static void main(String[] args) {
            new User().show();
            new User("叶子").show();
            new User("李阿昀", "liayun").show();
        }
    
    }

       6、

    public class StringTest {
    
        public static void main(String[] args) {
            getTime("Java 技术学习班 20070326");
            replace("MLDN JAVA", "JAVA", "J2EE");
            getChar("Java 技术学习班 20070326", 8);
            clear_0("Java 技术学习班 20070326", '0');
            clear_space("Java 技术学习班 20070326");
            getBirthday("429006199210061852");
        }
        //(1)
        public static void getTime(String str) {
            int index = str.indexOf('2');
            int year = Integer.parseInt(str.substring(index, index + 4));
            int month = Integer.parseInt(str.substring(index + 4, index + 6));
            int day = Integer.parseInt(str.substring(index + 6));
            System.out.println("开班日期:"+year+"年"+month+"月"+day+"日");
        }
        //(2)
        public static void replace(String str, String oldStr, String newStr) {
            System.out.println("替换之后的字符串为:"+str.replaceAll(oldStr, newStr));
        }
        //(3)
        public static void getChar(String str, int index) {
            System.out.println("""+str+"""+"中第"+index+"个字符为:"+str.charAt(index));
        }
        //(4)
        public static void clear_0(String str, char ch) {
            str = str.replaceAll("0", "");
            System.out.println("""+str+"""+"清除所有的0后为:"+str);
        }
        //(5)
        public static void clear_space(String str) {
            String[] s = str.split(" ");
            System.out.print("""+str+"""+"清除所有的空格后为:");
            for(int x = 0; x < s.length; x++) {
                System.out.print(s[x]);
            }
            System.out.println();
        }
        //(6)
        public static void getBirthday(String birthday) {
            int year = Integer.parseInt(birthday.substring(6, 10));
            int month = Integer.parseInt(birthday.substring(10, 12));
            int day = Integer.parseInt(birthday.substring(12, 14));
            
            System.out.println("身份证号为("+birthday+")人的生日为:"+year+"年"+month+"月"+day+"日");
        }
    
    }

       7、

    package chapter5;
    class Staff {
        private String id;
        private String name;
        private double salary;
        private String department;
        
        public Staff(String id) {
            this.id = id;
            name = "无名氏";
            salary = 0;
            department = "未定";
        }
    
        public Staff(String id, String name) {
            this.id = id;
            this.name = name;
            salary = 1000;
            department = "后勤";
        }
    
        public Staff(String id, String name, double salary, String department) {
            this.id = id;
            this.name = name;
            this.salary = salary;
            this.department = department;
        }
        
        public Staff() {
            
        }
            
        public void show() {
            System.out.println("员工信息为--->工号:"+id+",姓名:"+name+",薪水:"+salary
                    +",部门:"+department);
        }    
    }
    public class StaffTest {
    
        public static void main(String[] args) {
            new Staff().show();
            new Staff("001").show();
            new Staff("002", "李阿昀").show();
            new Staff("003", "李子", 25000.5, "研发部").show();
        }
    }

       8、

    package chapter5;
    
    public class Account {
        private String accontName;
        private double balance;
        
        public Account(String accontName, double balance) {
            this.accontName = accontName;
            this.balance = balance;
        }
        
        public double getBalance() {
            return balance;
        }
        
    }

       9、

    class Book {
        private String title;
        private String id;
        private double price;
        private static int count = 0;//静态数据成员册数
        
        Book(String title, double price) {
            this.title = title;
            this.id = "00"+(++count);
            this.price = price;
        }
    
        public String getTitle() {
            return title;
        }
    
    
        public void setTitle(String title) {
            this.title = title;
        }
    
    
        public String getId() {
            return id;
        }
    
    
        public void setId(String id) {
            this.id = id;
        }
    
    
        public double getPrice() {
            return price;
        }
    
    
        public void setPrice(double price) {
            this.price = price;
        }
        
        public static int getCount() {
            return count;
        }
        
        
    }
    public class BookTest {
    
        public static void main(String[] args) {
            Book[] books = {new Book("小王子", 34.5),
                            new Book("西游记", 56.5),
                            new Book("Thinking In Java", 34.5)};
            
            for(int x = 0; x < books.length; x++) {
                System.out.println("编号:"+books[x].getId()+",书名:"+books[x].getTitle()+
                                    "价格:"+books[x].getPrice());
            }
            System.out.println("图书的总册数为:"+Book.getCount()+"册");
        }
    
    }
  • 相关阅读:
    CodeForces Gym 100500A A. Poetry Challenge DFS
    CDOJ 486 Good Morning 傻逼题
    CDOJ 483 Data Structure Problem DFS
    CDOJ 482 Charitable Exchange bfs
    CDOJ 481 Apparent Magnitude 水题
    Codeforces Gym 100637G G. #TheDress 暴力
    Gym 100637F F. The Pool for Lucky Ones 暴力
    Codeforces Gym 100637B B. Lunch 找规律
    Codeforces Gym 100637A A. Nano alarm-clocks 前缀和
    TC SRM 663 div2 B AABB 逆推
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5236692.html
Copyright © 2020-2023  润新知