01:设计一个Dog类,有名字、颜色和年龄属性,提供无参的构造方法和一个有参的构造方法,定义输出方法show()显示其信息。
使用有参构造方法创建一个对象,并输出该对象的信息。
- package cn.bdqn.Lianxi;
-
- public class Dog {
- private String name;
- private String colour;
- private int age;
- public Dog() {
- }
- public Dog(String name, String colour, int age) {
- this.name = name;
- this.colour = colour;
- this.age = age;
- };
- public void show(){
- System.out.println("宠物信息:名字:"+this.name+",颜色:"+this.colour+",年龄:"+this.age);
- }
- }
-
-
-
-
-
-
-
-
-
-
- package cn.bdqn.Lianxi;
-
- public class DogTest {
-
- public static void main(String[] args) {
- Dog dog = new Dog("tony","粉色",5);
- dog.show();
-
- }
- }
02:写一个人的类
属性:名字,性别,年龄;提供无参的构造方法和一个有参的构造方法
使用有参构造方法创建一个对象:姓名为“张三”,性别为“男”,年龄25.
- package cn.bdqn.Lianxi;
-
- package cn.bdqn.Lianxi;
-
- public class Person {
- private String name;
- private char gender;
- private int age;
-
- public Person() {
- }
-
- public Person(String name, char gender, int age) {
- super();
- this.name = name;
- this.gender = gender;
- this.age = age;
- }
- public void printInfo() {
- System.out.println("姓名是:" + this.name + ",性别是:" +this.gender + ",年龄是:"
- + this.age);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- package cn.bdqn.Lianxi;
-
- public class PersonTest {
-
- public static void main(String[] args) {
- Person person = new Person("张三",'男',25);
- person.printInfo();
- }
-
- }
03:写一个汽车类:
属性:品牌;车长;颜色;价格;
提供无参的构造方法和一个有参的构造方法
使用有参构造方法创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝”
- package cn.bdqn.Lianxi;
-
- public class Car {
- private String paizi;
- private double length;
- private String colour;
- private int price;
- public Car() {
- }
- public Car(String paizi, double length, String cloro, int price) {
- this.paizi = paizi;
- this.length = length;
- this.colour = colour;
- this.price = price;
- }
- public void printInfo(){
- System.out.println("品牌是:"+this.paizi+",车长是:"+this.length+",颜色是:"+this.colour+",价格是:"+this.price);
- }
- }
-
-
-
-
-
-
-
-
-
- package cn.bdqn.Lianxi;
-
- public class CarTest {
-
- public static void main(String[] args) {
- Car car1 = new Car("捷达",500,"粉色",1000000);
- Car car2 = new Car("宝马",500,"粉色",1000000);
- Car car3 = new Car("劳斯莱斯",500,"粉色",1000000);
- Car car4 = new Car("科鲁兹",500,"粉色",1000000);
- Car car5 = new Car("迈锐宝",500,"粉色",1000000);
- car1.printInfo();
- car2.printInfo();
- car3.printInfo();
- car4.printInfo();
- car5.printInfo();
- }
- }
04:写一个课程类:
属性:课程名;学时;任课老师;
提供无参的构造方法和一个有参的构造方法
使用有参构造方法创建五个对象:“c语言”,“Java编程”,“php网络编程”,“c++”,“数据结构”
- package cn.bdqn.Lianxi;
-
- public class School {
- private String course;
- private int hour;
- private String teacher;
- public School() {
- }
-
- public School(String course, int hour, String teacher) {
- this.course = course;
- this.hour = hour;
- this.teacher = teacher;
- }
- public void printInfo(){
- System.out.println("课程名是:"+this.course+",学时:"+this.hour+",任课老师:"+this.teacher);
- }
- }
-
-
-
-
-
-
-
-
-
-
- package cn.bdqn.Lianxi;
-
- public class SchoolTest {
-
- public static void main(String[] args) {
- School school1 = new School("c语言",10,"张三");
- School school2 = new School("Java编程",10,"张三");
- School school3 = new School("php网络编程",10,"张三");
- School school4 = new School("c++",10,"张三");
- School school5 = new School("数据结构",10,"张三");
- school1.printInfo();
- school2.printInfo();
- school3.printInfo();
- school4.printInfo();
- school5.printInfo();
- }
-
- }
-
双击加关注啊、、、铁子们!!!!!!!!