• Java 入门21 java 进阶大纲 及复习


     

     

    回顾类 对象  构造器 ArrayList的基本使用

    Animal类

    package com.renzen.Animal.Day1;
    
    public class Animal {
        private String name;
        private String sex;
    
        //有参构造器
    
        public Animal(String name, String sex) {
            this.name = name;
            this.sex = sex;
        }
    
    
        //无参构造器
    
        public Animal() {
        }
    
        //get set
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    }
    View Code

    Test

    package com.renzen.Animal.Day1;
    
    import java.util.ArrayList;
    
    public class Test {
        public static void main(String[] args) {
            //创建动物类ArrayList 集合
            ArrayList<Animal> list=new ArrayList<>();
    
            //创建Animal实例
            Animal animal=new Animal("金毛","公");
            Animal animal1=new Animal("牧羊犬","母");
    
            list.add(animal);
            list.add(animal1);
            for (int i = 0; i < list.size(); i++) {
                System.out.println("品种 \t" + list.get(i).getName() + "性别\t" + list.get(i).getSex());
            }
        }
    }
    View Code

      

    Car 类

    package com.renzen.Animal.Day1;
    
    public class Car {
        private String name;
        private double price;
    
        //无参构造器
        public Car() {
        }
    
    
        //有参构造器
        public  Car(String name,double price){
            //如果不用this  则name不能给name赋值
            //如果不用this  则price不能给price赋值
            //name=name;
            //price=price;
    
            this.name=name;
            this.price=price;
        }
    
        //run
        public void run(String name){
            System.out.println(this.name+"在和"+name+"比赛");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    }
    View Code

    Test

    package com.renzen.Animal.Day1;
    
    public class Test1 {
        public static void main(String[] args) {
            Car c1=new Car("张三",121212);
            //当类名中不用this指定时
            System.out.println(c1.getName());//null
            System.out.println(c1.getPrice());//0.0
    
            System.out.println(c1.getName());//张三
            System.out.println(c1.getPrice());//121212
    
            c1.run("宝马");
        }
    }
    View Code

     封装 个人 理解 就是 private 私有化成员属性  对外暴露set/get方法堆私有属性进行修改

    Person类

    package com.renzen.Animal.Day1;
    
    public class Person {
        private int age;
    
        //无参构造器
        public Person(){
    
        }
        //有参构造器
        public void Person(int age){
            this.age=age;
        }
    
        //get set
        public void setAge(int age){
            if(age>0 && age<100){
                this.age=age;
            }else{
                System.out.println("age输入值有问题");
            }
        }
        public int getAge(){
            return this.age;
        }
    }
    View Code

    Test类、

    package com.renzen.Animal.Day1;
    
    public class Test3 {
        public static void main(String[] args) {
            Person p=new Person();
            System.out.println(p.getAge());//0
    
            p.setAge(18);
            System.out.println(p.getAge());//18
        }
    }
    View Code

  • 相关阅读:
    别闹了,这些都不是数字化转型
    对不起,“下一代ERP”仍旧是现在的ERP
    这世界真小
    SAP S4HANA 2020 Fully-Activated Appliance 虚拟机版分享
    花费巨资参加SAP培训真的有用吗?
    剑指 Offer 07. 重建二叉树
    剑指 Offer 06. 从尾到头打印链表
    剑指 Offer 05. 替换空格
    剑指 Offer 04.二维数组中的查找
    剑指 Offer 03. 数组中重复的数字
  • 原文地址:https://www.cnblogs.com/lvlisn/p/16409534.html
Copyright © 2020-2023  润新知