《Java程序设计》实验报告(二)之面向对象编程基础

实验内容及步骤:

  1. 编写不带构造函数的类并测试。(学生类、圆类)

(1)代码:

class Student {

    String name="张三";

    int age=20;

    String sex="男";//gender

    String getName(){

        return name;

    }

    void setName(String newName)

    {

        name=newName;

    }

    void read()

    {

        System.out.println("我是"+name+"年龄:"+age+",我正在学习!");

    }

    void c(Student student)

    {

        student.name="王琳";

        student.age=200;

        student.sex="male";

    }

    public static void main(String[] args)

    {

        Student student=null;

        student=new Student();

        System.out.println(student);

        student.read();

        Student student1=new Student();

实验内容及步骤:     

        System.out.println(student1);

        student1.read();

        Student student2=student;

        System.out.println(student2);

    }

}

Test:

public class Test

{

    public static void main(String[] args)

    {

        Student student=new Student();

        Student s=new Student();

        student.name="李四";

        student.setName("李四");

        student.age=22;

        student.sex="女";

        System.out.println(student.getName());

        student.read();

        student.c(student);

        student.read();

        s.name="张三";

        s.age=100;

        s.sex="female";

        student=s;

        student.read();

        s.read();

    }

}

(2)运行结果:

  1. 编写带构造函数的类。(学生类、圆类)

(1)代码:

实验内容及步骤:

public class Circle

{

    private double radius;

    // 构造函数

    public Circle(double radius)

    {

        this.radius = radius;

    }

    public double getRadius()

    {

        return radius;

    }

    public double getDiameter()

    {

        return 2 * radius;

    }

    public double getCircumference()

    {

        return 2 * Math.PI * radius;

    }

    public double getArea()

    {

        return Math.PI * radius * radius;

    }

}

Test:

public class Test {

    public static void main(String[] args) {

        Circle circle = new Circle(5.0);

        System.out.println("圆的半径:" + circle.getRadius());

        System.out.println("圆的直径:" + circle.getDiameter());

        System.out.println("圆的周长:" + circle.getCircumference());

        System.out.println("圆的面积:" + circle.getArea());

    }

}

(2)运行结果:

实验内容及步骤:

  1. 编写有静态方法的类。(求最大值、求和)

(1)代码:

public class MathUtils

{

    public static int findMax(int[] numbers)

    {

        if (numbers == null || numbers.length == 0)

        {

            throw new IllegalArgumentException("数组不能为空");

        }

        int max = numbers[0];

        for (int i = 1; i < numbers.length; i++)

        {

            if (numbers[i] > max)

            {

                max = numbers[i];

            }

        }

        return max;

    }

    public static int sum(int[] numbers)

    {

        if (numbers == null || numbers.length == 0)

        {

            throw new IllegalArgumentException("数组不能为空");

        }

        int sum = 0;

        for (int number : numbers)

        {

            sum += number;

实验内容及步骤:

}

        return sum;

    }

}

Test:

public class Test {

    public static void main(String[] args) {

        int[] numbers = {2, 5, 1, 8, 4};

        int maxValue = MathUtils.findMax(numbers);

        System.out.println("最大值:" + maxValue);

        int sumValue = MathUtils.sum(numbers);

        System.out.println("求和结果:" + sumValue);

    }

}

  1. 运行结果:

4、编写对包操作的程序。(学生类及测试类分别放到两个包里)

(1)代码:

public class Student
{
    private String name;
    private int age;
    public Student(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    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;
    }
}

Test:

public class Test {
    public static void main(String[] args) {
        Student person1 = new Student("张三", 20);
        System.out.println("姓名:" + person1.getName());
        System.out.println("年龄:" + person1.getAge());
        Student person2 = new Student("李四", 25);
        System.out.println("姓名:" + person2.getName());
        System.out.println("年龄:" + person2.getAge());
    }
}

(2)运行结果:

你的问题:

  1. 静态成员访问非静态成员的限制:在静态方法中只能访问静态成员,无法直接访问非静态成员。
  2. 编写带构造函数的类时,遇到构造函数调用错误、对象实例化错误问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

了一li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值