Java面向对象编程有哪些基础练习可以学习?

更新于
2026-10-12 09:24:53
1阅读来源:SEO教程
  • 内容介绍
  • 相关推荐

本文共计245个文字,预计阅读时间需要1分钟。

Java面向对象编程有哪些基础练习可以学习?

(1)定义描述学生的类Student,包含一个构造方法进行属性初始化,一个output方法用于输出学生信息。

javapublic class Student { private String name; private int age;

public Student(String name, int age) { this.name=name; this.age=age; }

public void output() { System.out.println(Name: + name + , Age: + age); }}

(2)定义主类,创建两个Student对象,测试其功能。

Java面向对象编程有哪些基础练习可以学习?

javapublic class Main { public static void main(String[] args) { Student student1=new Student(Alice, 20); Student student2=new Student(Bob, 22);

student1.output(); student2.output(); }}

(1)定义描述学生的类Student,有一个构造方法对属性 进行初始化,一个outPut方法用于输出学生的信息。 (2)定义主类,创建两个Student类的对象,测试其功能。

public class Student { String name; int age; char gender; Student(String name,int age,char gender){ this.name=name; this.age=age; this.gender=gender; } void output(){ System.out.print(this.name +" "+this.age+" "+this.gender); } } public class text3 { public static void main(String[] args) { Student myStudent1=new Student("刘妍", 21, 'F'); myStudent1.output(); System.out.println(); Student myStudent2=new Student("jack", 20, 'M'); myStudent2.output(); } }

本文共计245个文字,预计阅读时间需要1分钟。

Java面向对象编程有哪些基础练习可以学习?

(1)定义描述学生的类Student,包含一个构造方法进行属性初始化,一个output方法用于输出学生信息。

javapublic class Student { private String name; private int age;

public Student(String name, int age) { this.name=name; this.age=age; }

public void output() { System.out.println(Name: + name + , Age: + age); }}

(2)定义主类,创建两个Student对象,测试其功能。

Java面向对象编程有哪些基础练习可以学习?

javapublic class Main { public static void main(String[] args) { Student student1=new Student(Alice, 20); Student student2=new Student(Bob, 22);

student1.output(); student2.output(); }}

(1)定义描述学生的类Student,有一个构造方法对属性 进行初始化,一个outPut方法用于输出学生的信息。 (2)定义主类,创建两个Student类的对象,测试其功能。

public class Student { String name; int age; char gender; Student(String name,int age,char gender){ this.name=name; this.age=age; this.gender=gender; } void output(){ System.out.print(this.name +" "+this.age+" "+this.gender); } } public class text3 { public static void main(String[] args) { Student myStudent1=new Student("刘妍", 21, 'F'); myStudent1.output(); System.out.println(); Student myStudent2=new Student("jack", 20, 'M'); myStudent2.output(); } }