• 方法(构造方法)与方法重载


    01:设计一个Dog类,有名字、颜色和年龄属性,提供无参的构造方法和一个有参的构造方法,定义输出方法show()显示其信息。

    使用有参构造方法创建一个对象,并输出该对象的信息。

    1. package cn.bdqn.Lianxi;
    2. public class Dog {
    3. private String name;
    4. private String colour;
    5. private int age;
    6. public Dog() {
    7. }
    8. public Dog(String name, String colour, int age) {
    9. this.name = name;
    10. this.colour = colour;
    11. this.age = age;
    12. };
    13. public void show(){
    14. System.out.println("宠物信息:名字:"+this.name+",颜色:"+this.colour+",年龄:"+this.age);
    15. }
    16. }
    17. package cn.bdqn.Lianxi;
    18. public class DogTest {
    19. public static void main(String[] args) {
    20. Dog dog = new Dog("tony","粉色",5);
    21. dog.show();
    22. }
    23. }

    02:写一个人的类

    属性:名字,性别,年龄;提供无参的构造方法和一个有参的构造方法

    使用有参构造方法创建一个对象:姓名为“张三”,性别为“男”,年龄25.

    1. package cn.bdqn.Lianxi;
    2. package cn.bdqn.Lianxi;
    3. public class Person {
    4. private String name;
    5. private char gender;
    6. private int age;
    7. public Person() {
    8. }
    9. public Person(String name, char gender, int age) {
    10. super();
    11. this.name = name;
    12. this.gender = gender;
    13. this.age = age;
    14. }
    15. public void printInfo() {
    16. System.out.println("姓名是:" + this.name + ",性别是:" +this.gender + ",年龄是:"
    17. + this.age);
    18. }
    19. }
    20. package cn.bdqn.Lianxi;
    21. public class PersonTest {
    22. public static void main(String[] args) {
    23. Person person = new Person("张三",'男',25);
    24. person.printInfo();
    25. }
    26. }

    03:写一个汽车类:

    属性:品牌;车长;颜色;价格;

    提供无参的构造方法和一个有参的构造方法

    使用有参构造方法创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝”

    1. package cn.bdqn.Lianxi;
    2. public class Car {
    3. private String paizi;
    4. private double length;
    5. private String colour;
    6. private int price;
    7. public Car() {
    8. }
    9. public Car(String paizi, double length, String cloro, int price) {
    10. this.paizi = paizi;
    11. this.length = length;
    12. this.colour = colour;
    13. this.price = price;
    14. }
    15. public void printInfo(){
    16. System.out.println("品牌是:"+this.paizi+",车长是:"+this.length+",颜色是:"+this.colour+",价格是:"+this.price);
    17. }
    18. }
    19. package cn.bdqn.Lianxi;
    20. public class CarTest {
    21. public static void main(String[] args) {
    22. Car car1 = new Car("捷达",500,"粉色",1000000);
    23. Car car2 = new Car("宝马",500,"粉色",1000000);
    24. Car car3 = new Car("劳斯莱斯",500,"粉色",1000000);
    25. Car car4 = new Car("科鲁兹",500,"粉色",1000000);
    26. Car car5 = new Car("迈锐宝",500,"粉色",1000000);
    27. car1.printInfo();
    28. car2.printInfo();
    29. car3.printInfo();
    30. car4.printInfo();
    31. car5.printInfo();
    32. }
    33. }

    04:写一个课程类:

    属性:课程名;学时;任课老师;

    提供无参的构造方法和一个有参的构造方法

    使用有参构造方法创建五个对象:“c语言”,“Java编程”,“php网络编程”,“c++”,“数据结构”

    1. package cn.bdqn.Lianxi;
    2. public class School {
    3. private String course;
    4. private int hour;
    5. private String teacher;
    6. public School() {
    7. }
    8. public School(String course, int hour, String teacher) {
    9. this.course = course;
    10. this.hour = hour;
    11. this.teacher = teacher;
    12. }
    13. public void printInfo(){
    14. System.out.println("课程名是:"+this.course+",学时:"+this.hour+",任课老师:"+this.teacher);
    15. }
    16. }
    17. package cn.bdqn.Lianxi;
    18. public class SchoolTest {
    19. public static void main(String[] args) {
    20. School school1 = new School("c语言",10,"张三");
    21. School school2 = new School("Java编程",10,"张三");
    22. School school3 = new School("php网络编程",10,"张三");
    23. School school4 = new School("c++",10,"张三");
    24. School school5 = new School("数据结构",10,"张三");
    25. school1.printInfo();
    26. school2.printInfo();
    27. school3.printInfo();
    28. school4.printInfo();
    29. school5.printInfo();
    30. }
    31. }

    双击加关注啊、、、铁子们!!!!!!!!

  • 相关阅读:
    C++之虚函数与虚函数表
    Linux的OpenLava配置
    android CountDownTimer倒计时随时随地开启或关闭
    Ant Design Vue Pro去掉权限,直接进入系统
    软考高级信息系统项目管理师系列论文九:论信息系统项目的采购管理
    【数据结构】链表
    【智能优化算法-MOEA_D】基于MOEA_D求解联合经济排放调度(CEED)问题附matlab代码
    linux信息查询
    简单记录下gin中使用中间件记录操作日志
    Boost.Interprocess 的同步
  • 原文地址:https://blog.csdn.net/m0_58295980/article/details/126128043