1.数组的简单实现
/**
* Description: JavaPractice
*
* @Author: liuxiaofeng
* @Date Created in 2024/1/24 8:20
* @Date Modified by: liuxiaofeng on 2024/1/24
*/
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = new int[20];
for(int i=1;i< arr.length;i++){
System.out.print(arr[i]);
}
}
}
2.有参/无参
/**
* Description: JavaPractice
*
* @Author: liuxiaofeng
* @Date Created in 2024/1/24 16:37
* @Date Modified by: liuxiaofeng on 2024/1/24
*/
public class Car {
private String brand; // 品牌
private String color; // 颜色
private double price; // 价格
// 无参构造方法
public Car() {
}
// 三个参数的构造方法
public Car(String brand, String color, double price) {
this.brand = brand;
this.color = color;
this.price = price;
}
// 打印特征
public void printFeatures() {
System.out.println("品牌: " + brand);
System.out.println("颜色: " + color);
System.out.println("价格: " + price);
}
// 其他方法测试
public void testMethod() {
System.out.println("其他方法的测试");
}
public static void main(String[] args) {
// 使用无参形式构造对象并打印特征
Car car1 = new Car();
car1.printFeatures();
System.out.println("===================");
// 使用有参形式构造对象并打印特征
Car car2 = new Car("BMW", "黑色", 200000);
car2.printFeatures();
System.out.println("===================");
// 使用有参形式构造的对象调用其他方法测试
car2.testMethod();
}
}
3.简单的实体类和测试类
/**
* Description: JavaPractice
* 实体类 提供共有的方法 封装
* @Author: liuxiaofeng
* @Date Created in 2024/1/24 10:52
* @Date Modified by: liuxiaofeng on 2024/1/24
*/
public class Person {
private String name;
private int age;
public void show(){
System.out.println("姓名:"+name+",年龄:"+age);
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName(){
return name;
}
}
4.练习
**
* Description: JavaPractice
*
* @Author: liuxiaofeng
* @Date Created in 2024/1/24 16:31
* @Date Modified by: liuxiaofeng on 2024/1/24
*/
public class Rectangle {
private int width; // 宽度
private int length; // 长度
// 构造方法
// 求面积
public int getArea() {
return width * length;
}
// 求周长
public int getPer() {
return 2 * (width + length);
}
// 输出长、宽、面积、周长
public void showAll() {
System.out.println("长: " + length);
System.out.println("宽: " + width);
System.out.println("面积: " + getArea());
System.out.println("周长: " + getPer());
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public static void main(String[] args) {
// 创建一个Rectangle对象并初始化
Rectangle rectangle = new Rectangle();
// 输出相关信息
rectangle.showAll();
}
}