1.24java

本文介绍了Java编程中的数组使用、基本数据类型数组的初始化、数组遍历,以及类的实例化、构造方法、成员变量、方法重载、封装和单元测试的基本概念和示例。
摘要由CSDN通过智能技术生成

数组

1.部分数组默认值

public class Demo24a1 {
    public static void main(String[] args) {
        int[] arr = new int[5];
        System.out.println(arr[0]);
        boolean[] brr = new boolean[5];
        System.out.println(brr[0]);
        char[] crr = new char[5];
        if (crr[0] == 0){
            System.out.println("char数组默认值为0");
        }
        System.out.println(crr[0]);
    }
}

2.数组遍历

public class Demo24a2 {
    public static void main(String[] args) {
        int[] arr = new int[10];
        for (int i=0;i < arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

3.双色球案例(随机数,去重)

import java.util.Random;

//双色球案例
//7个号码,红球6个,范围1-33,蓝球1个,范围1-17
public class Demo24a3 {
    public static void main(String[] args) {
        int[] arr = new int[7];
        //创建随机数对象
        Random random = new Random();

        //开始摇号(向数组添加值)
        //摇红球
        for (int i = 0;i < arr.length-1;i++){
            arr[i] = random.nextInt(33) + 1;
            //去重
            for (int j = i - 1;j >= 0;j--){
                //表示号码重复
                if (arr[i] == arr[j]){
                    //若号码重复,需重新摇号
                    i--;
                    break;
                }

            }
        }
        //摇蓝球
        arr[arr.length - 1] = random.nextInt(17) + 1;

        //将数组中的双色球遍历
        System.out.print("本期中奖结果:");
        for (int i = 0;i < arr.length;i++){
            System.out.print(arr[i] + " ");
        }
    }
}

成员变量 

1.坐标

import java.awt.*;
public class Demo24a4 {
    int x;
    int y;

    public static void main(String[] args) {
        Point point = new Point();
        System.out.println(point.x);
        System.out.println(point.y);
        point.x = 3;
        point.y = 4;
        System.out.println(point.x);
        System.out.println(point.y);
    }
}

2.phone类,特征:brand和price

public class Demo24a5 {
    String brand;
    double price;
    public void show(){
        System.out.println("品牌:" + brand + ",价格:" + price);
    }
    public static void main(String[] args) {
        Demo24a5 p = new Demo24a5();
        p.show();
        p.brand ="Nokia";
        p.price = 555;
        p.show();
    }
}

3.Person类,特征:name和age

public class Demo24a6 {
    String name;
    int age;
    public void show(){
        System.out.println("姓名:" + name + ",年龄:" + age);
    }
    public static void main(String[] args) {
        Demo24a6 p = new Demo24a6();
        p.name = "gao";
        p.age = 18;
        p.show();
    }
}

 4.返回值,形参

public class Demo24a7 {
    public int add(int a,int b){
        return a+b;
    }
    public static void main(String[] args) {
        Demo24a7 D = new Demo24a7();
        int sum = D.add(2,3);
        System.out.println("sum = " + sum);
    }
}

5.get set方法

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
//girl类
public class Demo24a8 {
    String name;
    int age;
    boolean bf;
    public void show(){
        System.out.println("姓名:" + name + ",年龄:" + age + ",有没有男盆友:" + bf);
    }

    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;
    }
    public boolean isBf() {
        return bf;
    }
    public void setBf(boolean bf) {
        this.bf = bf;
    }
    public static void main(String[] args) {
        Demo24a8 d= new Demo24a8();
        d.name = ("貂蝉");
        d.age = 18;
        d.bf = true;
        d.show();
    }
}

 构造方法和方法重载

当用户没有书写任何形式的构造方法时,系统会自动提供一个无参的构造方法。
当用户书写了构造方法时,无论是有参还是无参,系统都不会提供任何形式的构造方法。

 1.Point

public class Point {
    int x;
    int y;
    public Point() {
        this.x = 0;
        this.y = 0;
    }
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void show() {
        System.out.println("Point: (" + x + ", " + y + ")");
    }
    public void increaseX() {
        x++;
    }
    public void increaseY(int z) {
        y += z;
    }
    public static void main(String[] args) {
        Point point1 = new Point();
        point1.show();
        Point point2 = new Point(3, 5);
        point2.show();
        point2.increaseX();
        point2.increaseY(2);
        point2.show();
    }
}

 2.Circle

public class Circle {
    double r;
    double PI = 3.14;

    public Circle() {
        this.r = 3;
    }
    public double area() {
        return PI * r * r;
    }
    public double perimeter() {
        return 2 * PI * r;
    }

    public static void main(String[] args) {
        Circle c = new Circle();
        System.out.println("圆的面积为: " + c.area());
        System.out.println("圆的周长为: " + c.perimeter());
    }
}

3.Vehicle

public class Vehicle {

        double speed;
        String type;
        public Vehicle(double speed, String type) {
            this.speed = speed;
            this.type = type;
        }
        public void move() {
            System.out.println("Vehicle is moving at a speed of " + speed);
        }
        public void setSpeed(double s) {
            speed = s;
        }
        public void speedUp(double s) {
            speed += s;
        }
        public void speedDown(double s) {
            speed -= s;
        }
        public void show() {
            System.out.println("Type: " + type + ", Speed: " + speed);
        }
        public static void main(String[] args) {
            Vehicle vehicle = new Vehicle(60, "Car");
            vehicle.show();
            vehicle.speedUp(20);
            vehicle.show();
            vehicle.speedDown(10);
            vehicle.show();
        }
}

 4.Rectangle

public class Rectangle {
    int length;
    int width;

    public Rectangle(int width, int length) {
        this.width = width;
        this.length = length;
    }

    public int getArea() {
        return length * width;
    }

    public int getPer() {
        return 2 * (length + width);
    }

    public void showAll() {
        System.out.println("Width: " + width);
        System.out.println("Length: " + length);
        System.out.println("Area: " + getArea());
        System.out.println("Perimeter: " + getPer());
    }
}


 5.Car

public class Car {

        String brand;
        String color;
        double price;
        public Car() {

        }
        public Car(String brand, String color, double price) {
            this.brand = brand;
            this.color = color;
            this.price = price;
        }
        public void show() {
            System.out.println("Brand: " + brand);
            System.out.println("Color: " + color);
            System.out.println("Price: " + price);
        }
        public String getBrand() {
            return brand;
        }
        public void setBrand(String brand) {
            this.brand = brand;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public void increasePrice() {
            price += 1000;
        }
        public void increasePrice(double amount) {
            price += amount;
        }
        public static void main(String[] args) {
            Car car1 = new Car();
            car1.show();
            Car car2 = new Car("Toyota", "Red", 20000);
            car2.show();
            car2.setBrand("Honda");
            car2.setColor("Blue");
            car2.setPrice(25000);
            car2.show();
            car2.increasePrice();
            car2.show();
            car2.increasePrice(500);
            car2.show();
        }
    }

封装(私有)

person类

//封装(私有)
public class Person {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if(age > 0 && age < 150){
        this.age = age;
        }else {
            this.age = 18;
            System.out.println("你输入的年龄有误");
        }
    }
    public void show(){
        System.out.println("姓名:" + name + ",年龄:" + age);
    }
}

测试类:单元测试shift+ctrl+T,加注释@Test,person类错误解决

import org.junit.Test;
//单元测试shift+ctrl+T,加注释@Test
import static org.junit.Assert.*;

public class PersonTest {
    @Test
    public void test01(){
    Person person = new Person();
    person.show();
    person.setName("张飞");
    person.setAge(-1);
    person.show();
 }

}

 

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值