Java 面向对象案例 03(黑马)

Java 面向对象案例 03(黑马)_第1张图片

代码:

public class phoneTest {
    public static void main(String[] args) {

        phone [] arr = new phone[3];

        phone p1 = new phone("华为",6999,"白色");
        phone p2 = new phone("vivo",4999,"蓝色");
        phone p3 = new phone("苹果",7999,"黑色");

        arr[0]=p1;
        arr[1]=p2;
        arr[2]=p3;

        double avg = (p1.getPrice()+ p2.getPrice()+p3.getPrice())/3;
        System.out.println(avg);

    }
}
public class phone {
    private String brand;
    private double price;
    private String color;

    public phone() {
    }

    public phone(String brand, double price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

Java 面向对象案例 03(黑马)_第2张图片

运行结果:

Java 面向对象案例 03(黑马)_第3张图片

Java 面向对象案例 03(黑马)_第4张图片

代码:

public class girlTest {
    public static void main(String[] args) {
        girl [] arr = new girl[4];

        girl g1 = new girl("小红",18,'女',"跳舞");
        girl g2 = new girl("小芳",22,'女',"唱歌");
        girl g3 = new girl("小刘",23,'女',"骑车");
        girl g4 = new girl("小美",19,'女',"爬山");

        arr[0]=g1;
        arr[1]=g2;
        arr[2]=g3;
        arr[3]=g4;

        double sum=0;
        for(int i=0;i
public class girl {
    private String name;
    private int old;
    private char sex;
    private String hobby;

    public girl() {
    }

    public girl(String name, int old, char sex, String hobby) {
        this.name = name;
        this.old = old;
        this.sex = sex;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getOld() {
        return old;
    }

    public void setOld(int old) {
        this.old = old;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

代码结果:

Java 面向对象案例 03(黑马)_第5张图片

Java 面向对象案例 03(黑马)_第6张图片

自己写的代码:

import java.sql.SQLOutput;
import java.util.Scanner;
public class studentTest {
    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        student [] arr = new student[3];

        student s1 = new student("heima001","小白",19);
        student s2 = new student("heima002","小黑",19);
        student s3 = new student();

        arr[0]=s1;
        arr[1]=s2;
        arr[2]=s3;

        System.out.println("输入第3个同学的学号:");

        boolean flag = true;
        do {
            String n = input.next();
            if ((!n.equals(s1.getNov())) && (!n.equals(s2.getNov()))) {
                s3.setNov(n);
                flag=false;
            } else {
                System.out.println("该用户已存在,输入失败,请重新输入:");
            }
        }while(flag);
        System.out.println("输入第3个同学的名字:");
        String m = input.next();
        s3.setName(m);
        System.out.println("输入第3个同学的年龄:");
        int q = input.nextInt();
        s3.setAge(q);

        for(int i=0;i
public class student {
    private String Nov;
    private String name;
    private int age;

    public student() {
    }

    public student(String nov, String name, int age) {
        Nov = nov;
        this.name = name;
        this.age = age;
    }

    public String getNov() {
        return Nov;
    }

    public void setNov(String nov) {
        Nov = nov;
    }

    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;
    }
}

缺少如果数组满了要添加对象进去的判断:

1、判断id是否重复:

Java 面向对象案例 03(黑马)_第7张图片

2、判断数组是否已满:

Java 面向对象案例 03(黑马)_第8张图片

3、如果数组已经满了,那么创建一个新数组,并且将老数组里的元素复制进去:

Java 面向对象案例 03(黑马)_第9张图片

4、进行添加和判断操作

Java 面向对象案例 03(黑马)_第10张图片

5、打印元素的时候,需要分两种情况讨论,一个是添加成功的数组,一个是没有添加成功的数组,可以写一个方法:

Java 面向对象案例 03(黑马)_第11张图片

6、在上面调用遍历的方法:

Java 面向对象案例 03(黑马)_第12张图片

若数组不满,删除stu3,那么在数组中事null,但是不能通过null直接使用遍历,相当于用null调用了其他方法,会爆粗,

Java 面向对象案例 03(黑马)_第13张图片

所以对判断唯一性的方法进行修改:需要判断stu是否为null

Java 面向对象案例 03(黑马)_第14张图片

补充:拆分题干:

Java 面向对象案例 03(黑马)_第15张图片

7、对于判断id写一个方法:

Java 面向对象案例 03(黑马)_第16张图片

8、删除id并遍历:

Java 面向对象案例 03(黑马)_第17张图片

9、给指定那个学号的学生年龄加一

Java 面向对象案例 03(黑马)_第18张图片

你可能感兴趣的:(黑马Java,java,开发语言)