面向对象练习

Java 课程实验二 话不多说,直接上实验内容(最基础的实验) 网络应用开发上机2 定义一个汽车类,要求该类中至少包含5个私有的属性(为每个属性定义设置和获取方法),该类中至少包含两个构造方法(一个无参,一个有参),以及包含5~6个方法来描述汽车对象的行为(其中一个方法要能打印输出汽车对象的所有属性值)。 定义一... 阅读详情

目录

1. (LeetCode 第169题)给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数大于 n/2 的元素。

2. 综合案例(用户博客管理系统)

  1. (抽象类)定义一个抽象的"Role"类 有姓名 年龄 性别等成员变量

  2. (抽象类)品尝饮料

1. (LeetCode 第169题)给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数大于 n/2 的元素。

例如:[1,1,1,1,2,3,3],多数元素为1

方法1:排序数组,找最中间的数

如果将数组 nums 中的所有元素按照单调递增或单调递减的顺序排序,那么下标为nums.lenght/2的元素(下标从 0 开始)即最中间的数一定是众数。

方法2:抵消法

循环遍历数组,下一个数相同时计数加1,下一个数不同计数就减去1,当为count==0时,把num[i]赋值给众数,直到最后,得到的数就是众数

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">import java.util.Arrays;

public class FindRepeatNum {
    public static void main(String[] args) {
//      int[] nums = { 1, 1, 1, 1, 2, 3, 3 };
        int[] nums = { 2, 2, 1, 1, 1, 2, 2 };
//      int[] nums = { 1, 3, 1, 1, 4, 1, 1, 5, 1, 1, 6, 2, 2 };
        int result = findRepeatMax(nums);
        System.out.println("出现次数大于" + nums.length / 2 + "的元素为" + result);
        int result1 = majorElements(nums);
        System.out.println("出现次数大于" + nums.length / 2 + "的元素为" + result1);
    }

    /*
     * 给定一个大小为 n 的数组 nums,返回其中的多数元素。
     * 多数元素是指在数组中出现次数大于 n/2 的元素。
     */
    private static int findRepeatMax(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }

    // nums[i]  1 2 2 1 1 1 1 2 3
    // major    1 / 2 / 1 1 1 1 1
    // count    1 0 1 0 1 2 3 2 1

    // nums[i]  1 2 2 1 1 1 1 2 3
    // major    1 2 2 2 1 1 1 1 1
    //            0     0
    // count.   1 1 2 1 1 2 3 2 1
    private static int majorElements(int[] nums) {
        int majority = nums[0];
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (count == 0) {
                count++;
                majority = nums[i];
            } else if (nums[i] == majority) {
                count++;
            } else if (nums[i] != majority) {
                count--;
            }
//          if (count == 0) {
//              count++;
//              majority = nums[i];
//          }
        }
        return majority;
    }
}</pre>

结果:

2. 综合案例(用户博客管理系统)

Bolg类(抽象类):

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer1;

public abstract class Blog {
    final static int Board = 1; // 板块
    final static int Article = 2; // 文章
    final static int Author = 3; // 作者
    final static int Comment = 4; // 作者

    // 抽象方法:showPage()页面展示
    public abstract void showPage();
}</pre>

Board类:

<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer1;

//import java.util.Arrays;

public class Board extends Blog {
    private int id; // 板块id
    private String name; // 板块名称
    private String intro; // 简介或描述

//  开发中尽量使用单项关联,双向关联会造成冗余
//  private Article[] articles;

    @Override
    public String toString() {
        return name;
    }

    public Board() {
        super();
    }

    public Board(int id, String name, String intro) {
        super();
        this.id = id;
        this.name = name;
        this.intro = intro;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }

    @Override
    public void showPage() {
        // TODO Auto-generated method stub
        System.out.println("********************模块详情********************");
        System.out.println("  作者编号:");
        System.out.println("\t" + this.getId());
        System.out.println("  作者姓名");
        System.out.println("\t" + this.getName());
        System.out.println("  作者简介:");
        System.out.println("\t" + this.getIntro());
        System.out.println("************************************************");
        System.out.println();
    }

}</pre>

Author类:

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer1;

//import java.util.Arrays;

public class Author extends Blog {
    private int id; // 作者id
    private String name; // 姓名
    private String gender; // 性别
    private int age; // 年龄
    private String intro = "作者很懒,什么业没留下~"; // 简介或描述
//  private Article[] articles;

    @Override
    public String toString() {
        return name;
    }

    public Author(int id, String name, String gender, int age, String intro) {
        super();
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.intro = intro;
    }

    public Author() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }

    @Override
    public void showPage() {
        // 作者详情页
        System.out.println("********************作者详情********************");
        System.out.println("  作者编号:");
        System.out.println("\t" + this.getId());
        System.out.println("  作者姓名");
        System.out.println("\t" + this.getName());
        System.out.println("  作者性别:");
        System.out.println("\t" + this.getGender());
        System.out.println("  作者年龄:");
        System.out.println("\t" + this.getAge());
        System.out.println("  作者简介:");
        System.out.println("\t" + this.getIntro());
        System.out.println("************************************************");
        System.out.println();
    }
}</pre>

Article类:

<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer1;

import java.util.Date;

//import java.util.Arrays;

public class Article extends Blog {
    private int id; // 文章id
    private String tittle; // 标题
    private String content; // 内容
    private Author Author; // 作者
    private Board board; // 所属板块
    private Date publishTime; // 发布时间
    private Date modifyTime; // 修改时间
    @Override
    public String toString() {
        return tittle;
    }

    public Article(int id, String tittle, String content, Author Author, Board board, Date publishTime,
            Date modifyTime) {
        super();
        this.id = id;
        this.tittle = tittle;
        this.content = content;
        this.Author = Author;
        this.board = board;
        this.publishTime = publishTime;
        this.modifyTime = modifyTime;
    }

    public Article() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTittle() {
        return tittle;
    }

    public void setTittle(String tittle) {
        this.tittle = tittle;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Author getAuthor() {
        return Author;
    }

    public void setAuthor(Author Author) {
        this.Author = Author;
    }

    public Board getBoard() {
        return board;
    }

    public void setBoard(Board board) {
        this.board = board;
    }

    public Date getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    @Override
    public void showPage() {
        // 文章详情页
        System.out.println("********************文章详情********************");
        System.out.println("  文章编号:");
        System.out.println("\t" + this.getId());
        System.out.println("  文章标题:");
        System.out.println("\t" + this.getTittle());
        System.out.println("  文章作者:");
        System.out.println("\t" + this.getAuthor());
        System.out.println("  发布时间:");
        System.out.println("\t" + this.getPublishTime());
        System.out.println("  修改时间:");
        System.out.println("\t" + this.getModifyTime());
        System.out.println("  文章内容:");
        System.out.println("\t" + this.getContent());
        System.out.println("  所属板块:");
        System.out.println("\t" + this.getBoard());
        System.out.println("************************************************");
        System.out.println();
    }
}</pre>

Comment类:

<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer1;

import java.util.Date;

public class Comment extends Blog {
    private int id; // 回复id
    private String content; // 内容
    private Author Author; // 作者
    private Date publishTime; // 发布时间
    private Date modifyTime; // 修改时间
    private Article article; // 文章

    @Override
    public String toString() {
        return content;
    }

    public Comment(int id, String content, Author Author, Date publishTime, Date modifyTime, Article article) {
        super();
        this.id = id;
        this.content = content;
        this.Author = Author;
        this.publishTime = publishTime;
        this.modifyTime = modifyTime;
        this.article = article;
    }

    public Comment() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Author getAuthor() {
        return Author;
    }

    public void setAuthor(Author Author) {
        this.Author = Author;
    }

    public Date getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }

    @Override
    public void showPage() {
        // 文章详情页
        System.out.println("********************评论详情********************");
        System.out.println("  评论编号:");
        System.out.println("\t" + this.getId());
        System.out.println("  评论作者:");
        System.out.println("\t" + this.getAuthor());
        System.out.println("  发布时间:");
        System.out.println("\t" + this.getPublishTime());
        System.out.println("  修改时间:");
        System.out.println("\t" + this.getModifyTime());
        System.out.println("  评论内容:");
        System.out.println("\t" + this.getContent());
        System.out.println("  所属文章:");
        System.out.println("\t" + this.getArticle());
        System.out.println("************************************************");
        System.out.println();
    }
}</pre>

Test类:

<pre class="prettyprint hljs gradle" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer1;

import java.util.Date;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Article article = new Article();
        Author author = new Author();
        Board board = new Board();
        Comment comment = new Comment();

        author.setId(100001);
        author.setName("zhang");
        author.setAge(20);
        author.setGender("男");
        author.setIntro("入门程序猿,请多指教!");

        board.setId(1);
        board.setName("Java");
        board.setIntro("Java面向对象编程,文章汇聚地");

        article.setId(1001);
        article.setTittle("面向对象三大特征");
        article.setAuthor(author);
        article.setPublishTime(new Date());
        article.setModifyTime(new Date());
        article.setContent("面向对象的三大特征主要为:封装、继承、多态\n" + "\t    1\. 封装\n" + "\t    2\. 继承\n" + "\t    3\. 多态\n");
        article.setBoard(board);
//      article.showPage();

        comment.setId(1);
        comment.setAuthor(author);
        comment.setPublishTime(new Date());
        comment.setModifyTime(new Date());
        comment.setContent("作者,可以详细说一下什么是封装吗\n");
        comment.setArticle(article);
        while (true) {
            Blog page = null;
            System.out.println("        用户博客管理系统");
            System.out.println("************************************************");
            System.out.println();
            System.out.println("\t\t1\. 模块详情");
            System.out.println();
            System.out.println("\t\t2\. 文章详情");
            System.out.println();
            System.out.println("\t\t3\. 作者详情");
            System.out.println();
            System.out.println("\t\t4\. 评论详情");
            System.out.println();
            System.out.println("\t\t5\. 退出");
            System.out.println();
            System.out.println("************************************************");
            System.out.print("请输入你的选项(1-5):");
            switch (scanner.nextInt()) {
            case Blog.Board:
                page = board;
                break;
            case Blog.Article:
                page = article;
                break;
            case Blog.Author:
                page = author;
                break;
            case Blog.Comment:
                page = comment;
                break;
            default:
                System.exit(1);
                break;
            }
            System.out.println();
            page.showPage();
        }

    }
}</pre>

结果:

3. (抽象类)定义一个抽象的"Role"类 有姓名 年龄 性别等成员变量

1. 要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有)

再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法

该方法不返回任何值 同时至少定义两个构造方法。Role类中要体现出this的几种用法。

2. 从Role类派生出一个"Employee"类 该类具有Role类的所有成员

构造方法除外 并扩展 salary成员变量 同时增加一个静态成员变量“职工编号 ID ”。

同样要有至少两个构造方法 要体现出this和super的几种用法

还要求覆盖play()方法 并提供一个final sing()方法。

3. "Manager"类继承"Employee"类 有一个final成员变量"vehicle"

4. 在main()方法中制造Manager和Employee对象,并测试这些对象的方法

Role类:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer2;

public abstract class Role {
    private String name;
    private int age;
    private String gender;

    @Override
    public String toString() {
        return "姓名:" + name + ",年龄:" + age + ",性别:" + gender + "]";
    }

    public Role() {
        super();
    }

    public Role(String name, int age, String gender) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    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;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public abstract void play();
}</pre>

Employee类:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer2;

public class Employee extends Role {
    private double salary;
    private static int id = 1001;

    @Override
    public String toString() {
        return "姓名:" + super.getName() + ",工号:" + Employee.id + ",年龄:" + super.getAge() + 
                ",性别:" + super.getGender() + ",薪水:" + this.salary;
    }

    public Employee() {
        super();
    }

    public Employee(String name, int id, int age, String gender, double salary) {
        super(name, age, gender); // 调用父类的构造方法,一定要放在方法的首个语句
        this.salary = salary;
        Employee.id = id;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public static int getId() {
        return id;
    }

    public static void setId(int id) {
        Employee.id = id;
    }

    @Override
    public void play() {
        System.out.println(super.getAge() + ",这个年纪就应该玩!");
;
    }

    public final void sing() {
        System.out.println(this.getName() + ",放声歌唱吧");
    }
}</pre>

Manager类:

<pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer2;

public class Manager extends Employee {
    private final String vehicle = "公交";

    @Override
    public String toString() {
        return "姓名:" + super.getName() + ",工号:" + super.getId() + ",年龄:" + super.getAge() + 
                ",性别:" + super.getGender() + ",薪水:" + super.getSalary() + ",交通工具:" + this.vehicle;
    }

    public String getVehicle() {
        return vehicle;
    }
}</pre>

Test类:

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer2;

public class Test {
    public static void main(String[] args) {
        Manager manager = new Manager();
        manager.setAge(20);
        manager.play();
        manager.setName("zhang");
        manager.sing();
        manager.setGender("男");
        manager.setSalary(10000);
        System.out.println(manager);
        System.out.println(manager.getVehicle());

        Employee employee1 = new Employee();
        employee1.setName("wang");
        employee1.setAge(30);
        employee1.setGender("男");
        employee1.setSalary(4000);
        System.out.println(employee1);
        employee1.play();
        employee1.sing();
        Employee employee = new Employee("li", 1008, 25, "男", 5000);
        System.out.println(employee);
        employee.play();
        employee.sing();

        Role r1 = new Manager();
        r1.setAge(18);
        r1.play();
//      r1.sing();
        System.out.println(r1);
    }
}</pre>

结果:

4. (抽象类)品尝饮料

1、建立一个Java抽象类Drink,应当

  • a、声明一个抽象方法taste() 该方法负责输出饮料的味道
  • b、声明int型常量来代表不同的饮料类型 咖啡、啤酒、牛奶
  • c、声明静态工厂方法getDrink(int drinkType) 根据传入的参数创建不同的饮料对象 并返回该对象 建议使用switch语句。该方法要求声明DrinkNotFoundException 当没有相对应的饮料类型时 抛出该异常。

2、建立Drink的具体子类

  • a、分别建立Drink的子类 Coffee 代表咖啡 Beer 代表啤酒 Milk 代表牛奶 ;
  • b、实现taste()方法 要求在控制台打印各自的味道特征。

3、建立异常类DrinkNotFoundException

  • a、继承Exception
  • b、在Drink的 方 法getDrink(int drinkType)中 声 明 引 发DrinkNotFoundException异常 当传入的参数没有相对应的饮料类型时 抛出异常。
  • c、在使用getDrink方法的类中捕捉该异常。

4、建立Test测试类 测试以上内容的正确性

  • a、编写main方法 通过命令行传参的方式传入某种饮料的类型。
  • b、在main方法中 调用Drink类的getDrink方法 获得相应的饮料对象。注意捕获DrinkNotFoundException。
  • c、然后调用该饮料对象的taste()方法 输出该饮料的味道。

5、编译程序 并运行。

注意事项:1、仔细审题 把题目要求理解准确 2、请注意多态性的运用 3、请注意代码的书写、命名符合规范 应有适当的注释

Drink类:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer3;

public abstract class Drink {
    // 声明int型常量来代表不同的饮料类型 咖啡、啤酒、牛奶
    final static int COOFFEE = 1; // 咖啡
    final static int BEER = 2; // 啤酒
    final static int MILK = 3; // 牛奶

    // 声明静态工厂方法getDrink(int drinkType) 
    // 根据传入的参数创建不同的饮料对象,并返回该对象
    public static Drink getDrink(int drinkType) throws DrinkNotFoundException {
        switch (drinkType) {
        case COOFFEE:
            Coffee coffee = new Coffee();
            return coffee;
        case BEER:
            Beer beer = new Beer();
            return beer;
        case MILK:
            Milk milk = new Milk();
            return milk;
        default:
            throw new DrinkNotFoundException("找不的对应类型的饮料!");
        }
    }

    // 声明一个抽象方法taste(),该方法负责输出饮料的味道
    public abstract void taste();
}</pre>

Coffee类:

<pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer3;

public class Coffee  extends Drink {

    // 实现taste()方法
    @Override
    public void taste() {
        System.out.println("咖啡是厚重的苦味");
    }

}</pre>

Beer类:

<pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer3;

public class Beer extends Drink {

    // 实现taste()方法
    @Override
    public void taste() {
        // 打印各自的味道特征。
        System.out.println("啤酒是略微的苦涩味道");
    }

}</pre>

MilK类:

<pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer3;

public class Milk extends Drink {

    // 实现taste()方法
    @Override
    public void taste() {
        System.out.println("牛奶奶香味特别浓的");
    }

}</pre>

DrinkNotFoundException异常类:

<pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">package com.openlab.day13.exer3;

// 建立异常类DrinkNotFoundException,继承Exception 
public class DrinkNotFoundException extends Exception {
    static final long serialVersionUID = -3387516993124229948L;

    public DrinkNotFoundException(String message) {
        super(message);
    }
}</pre>

Test类:

结果:

python面向对象练习题01 练习题一 # 1.定义一个Person类,它包含数据成员age, name和gender。 # a.从Person中派生一个类Employee,在新类中添加一个数据成员,存储个人的number. # b、再从Employee中派生一个类Executive,每个派生类都应该定义一个方法, # 来显示相关的信息(名称和类型,如”Fred Smith is an Employee”)。 # c.编写一个列表,包含3个Executive对象,2个一般的Employee对象,然后显示它们的信息。 代码展示: . 阅读详情

相关推荐

Java面向对象程序设计——练习2

练习-Java继承和多态之方法重写 练习-Java继承和多态之接口 练习-Java继承和多态之综合练习 练习-Java异常处理之try-catch之异常捕获 练习-Java继承和多态之abstract类

weixin_60732009的博客 1096

Java的基础面试题

浏览器第一次访问服务器,服务器会创建一个session,然后同时为该session生成一个唯一的会话的key,也就是sessionid,然后,将sessionid及对应的session分别作为key和value保存到缓存中,也可以持久化到数据库中,然后服务器再把sessionid,以cookie的形式发送给客户端。客户端将token保存起来,下次请求时,带着token,服务器收到请求后,然后会用相同的算法和密钥去验证token,如果通过,执行业务操作,不通过,返回不通过信息;如果说下标对应的位置上有链表。

a2_45688的博客 1527

java面向对象练习题-经典

绝对经典

Java习题2:在一个类中实现中实现两个字符串数组的逆序排序、两个整形数组的交集、两个浮点型数组的并集等方法

问题描述:定义一个类,该类中包含以下几个方法(静态): (1)实现两个字符串数组的逆序排序,输出结果为字符串数组; (2)求两个整形数组的交集; (3)求两个浮点型数组的并集。 解决思路: (1)实现两个字符串数组的逆序:定义一个新的字符串数组Str,长度为需要逆序的两个字符串数组Str1和Str2之和。将Str1和Str2先后逆序存入输出结果中,最后将输出结果作为返回值即可。 (2)求两个整形数组的交集:定义一个新的整型数组numbers,长度定义为两个整型数组numbers1和numbers2长度之和(

xfjssaw的博客 1204

数字IC后端设计实战:从floorplan到powerplan的优化策略

本文深入探讨了数字IC后端设计中从Floorplan到Powerplan的核心优化策略。文章强调了Floorplan作为布局基础的重要性,并详细解析了Powerplan规划,特别是针对低功耗设计中MTCMOS的应用与IR Drop分析。通过实战经验分享,提供了从模块摆放到电源网络构建、再到IR Drop问题层层递进的解决方案,旨在帮助后端工程师构建稳健高效的芯片物理设计。

weixin_28338005的博客 353

脑影像分析软件推荐 | JuSpace

JuSpace是一种允许基于 MRI 测量与核成像衍生估计的跨模态关联的工具包,它涵盖各种神经递质系统,包括多巴胺能、血清素能、去甲肾上腺素能和 GABA(γ-氨基丁酸) 神经传递。JuSpace 由一组 Matlab 功能以及涵盖各种受体系统的 PET 受体图谱组成。

NHKJ_ZF的博客 1711

定义一个类,该类中包含以下几个方法(静态): 实现两个字符串数组的逆序排序,输出结果为字符串数组; 求两个整形数组的交集; 求两个浮点型数组的并集;

​package Shiyan2; public class Test2 { public static void main(String[] args) { String str1[] = {"Hello","World!"}; String str2[] = {"abc","def"}; int a1[] = {1,2,3,4,5}; int a2[] = {6,7,8,9,0,10}; int a3[] = {1,2,3,4,5}; int a4[] = {5,6,

Lucas的博客 1148

ESP32之 ESP-IDF + Clion 开发环境搭建(二)—— Linux版

本文介绍了 Linux 下,使用Clion IDE 用官方 ESP-IDF 开发包进行 ESP32 开发的环境搭建

Augtons 的博客 6099

Java实验——定义一个类,该类中包含以下几个方法(静态):实现两个字符串数组的逆序排序,输出结果为字符串数组;求两个整形数组的交集;求两个浮点型数组的并集;

目录一、实现两个字符串数组的逆序排序,输出结果为字符串数组解题思路:具体代码:运行截图:二、求两个整形数组的交集具体代码:运行截图:三、求两个浮点型数组的并集解题思路:具体代码:运行截图:总具体代码定义一个方法,接受字符串类型的参数,首先利用lengrh()求出字符串的长度,然后利用toCharArray()方法将字符串转化为数组,再利用for循环,对字符数组进行逆序,最后利用valueof()将字符数组变回字符串。 运行截图: 解题思路:利用两个for循环,外层循环遍历第一个数组,内层循环遍历第

lil_ghost_的博客 936

RK3568下YT8531/YT8521 PHY移植避坑指南:RGMII与设备树详解

嵌入式Linux网络调试中,PHY芯片作为MAC与物理介质之间的桥梁,其驱动配置直接影响网络稳定性。RGMII接口的延时补偿、PHY地址设定以及复位时序是设备树配置中的关键环节,稍有差错便会出现链路已通却无法通信的现象。通过理解PHYLIB框架与mdio总线枚举原理,开发者可以快速定位寄存器级故障。在RK3568等工业级平台中,裕太微YT8531/YT8521凭借高性价比与内核主线支持,成为替代传统方案的热门选择。从设备树模板、内核配置到常见坑点,系统梳理千兆以太网PHY的移植与调试方法,帮助开发者少走弯路

weixin_29189381的博客 213

Android逆向分析

Android 逆向开发是指对已发布的 Android 应用进行分析和破解,以了解应用程序的内部工作原理,获取应用程序的敏感信息,或者修改应用程序的行为。Android应用 Java编写的,利用Android SDK编译代码,并并且把所有的数据和资源文件打包成一个APK(Android Package)文件这是一个后缀名为.apk的压缩文件,apk文件中包含了一个Anddroid应用程序的所有内容,是Android平台用于安装应用程序的文件。

qq_51598534的博客 6294

Java面向对象练习

下面给你几个练习题,覆盖封装、继承、多态、抽象和接口等知识点。

wincl0的博客 757

服务器测试之GPU基础汇总

GPU英文全称Graphic Processing Unit,中文翻译为“图形处理器”。一个专门的图形核心处理器。GPU是显示卡的“大脑”,决定了该显卡的档次和大部分性能,同时也是2D显示卡和3D显示卡的区别依据。可以形象的理解为90%的ALU(运算单元),5%的Control(控制单元)、5%的Cache(缓存单元)GPU的设计原理——总结为大量的简单运算。

qq_41897488的博客 2万+

C++面向对象练习

马上要期末了,复习的同时写一篇博客也巩固一下基础知识,这些练习虽然简单,但是一不小心还是会出错,比如在练习1中重载输出流是将<<写成>>,练习4中忘记预先分配数组内存,在练习7中忘记给类加public,所以就算是再简单的项目,再简单的题也要注意好细节,不然调试半天才发现竟然是自己的粗心导致的;另外,你如果感兴趣的话可以关注我,看看我其他的博客对你有没有帮助,我后续也会持续产出更高质量的博客。最后,希望这篇博客能帮助到你。

2401_88685396的博客 928

大语言模型与ROS集成的自然语言机器人控制:开源框架与应用实例

近年来,大语言模型(LLM)与机器人操作系统(ROS)的集成逐步成熟,使得自然语言驱动的机器人控制成为可能。用户输入“捡起绿色方块”或“把微波炉里的饭热好端过来”这类日常语句,机器人能够自动拆解步骤、规划动作并执行。本文梳理2026年前后发布的三个代表性开源框架——ROS-LLM、ROSClaw以及LLM+Nav2基准测试系统,介绍其技术路径与工程落地情况。

Studying…… 634

YOLO26 终极蒸馏指南:从入门到 SOTA (The Ultimate Distillation Guide)

《YOLO26 蒸馏技术指南》深入解析了知识蒸馏在目标检测中的应用,提出五大蒸馏范式:响应蒸馏(学习最终输出)、特征蒸馏(模仿中间特征)、关系蒸馏(捕捉通道关联)、CWD(通道语义对齐)和FGD(前景背景分离)。实验表明,"Response KD + CWD + FGD"组合效果最佳,使YOLO26n在0.5ms推理速度下达到0.956 mAP50,甚至超越教师模型。指南提供模块化代码实现和超参数调优建议,强调蒸馏能平衡模型精度与效率,是轻量化部署的有效方案。

weixin_51988935的博客 717

Java面向对象基础小练习。

    最后回顾一遍面向对象基础部分,后面就开始慢慢进阶了。    所有题目均来自:https://www.jianshu.com/p/886252693889    在练习中,有意识的规范代码格式,发现新东西,这才是练习的目的。    原帖中源代码就不贴上来了,贴上的的均为本人所编写,类、变量命名参考原帖。    【练习题】01.类的成员变量 猜数字游戏一个类A有一个成员变量v有一个初值100。...

yaerpi的博客 2169

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

python面向对象练习02

练习题一 # 5、某公司雇员(Employee)包括经理(Manager),技术人员(Technician)和销售员(Salesman)。 # 以Employee类为基类派生出Manager,Technician和Salesman类; # Employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。 # 操作包括月薪计算方法(pay()),该方法要求输入请假天数,扣去应扣工资后,得出实发基本工资。 # Technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度

qq_44850339的博客 815

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Python面向对象练习题

Python面向对象练习题 【下载地址】Python面向对象练习题 这份Python面向对象练习题资料专为编程学习者设计,尤其适合希望通过实践加深理解的教育工作者和学生。内容涵盖类与对象的定义、继承、多态等核心概念,每道练习题均附有详细解答和解析,助你轻松掌握知识点。通过独立完成练习并对照答案,你将有效提升Python面...

gitblog_06707的博客 434

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Java入门程序之面向对象基础练习

(1)声明一个圆的图形类,包含实例变量/属性:半径(2)在测试类的main中,创建2个圆类的对象,并给两个圆对象的半径属性赋值,最后显示两个圆的半径值、周长和面积值​ 提示:圆周率可以使用Math.PI。

m0_73334365的博客 1652

C# —— 面向对象编程练习

C# —— 面向对象编程练习 基础题 代码如下: class Program { static void Main(string[] args) { rectangle r = new rectangle(); Console.WriteLine("调用不含参构造函数初始化后矩形的长为{0},宽为{1}", r.GetLength().ToString(), r.GetWidth().ToString());

Deam_swan_goose的博客 5693

面向对象练习题【从零开始学Java】

从零开始学Java系列课程

波波烤鸭的博客 1172

Scala总结之基础练习和面向对象

Scala总结二之基础练习和面向对象一. scala基础练习1.1. 字符串转换为数字1.2. 使用scala完成选择排序1.3. 使用scala完成二分查找二. scala面向对象2.1. 类的基本操作2.1.1. 类的创建与对象的构造2.1.2. 成员变量的封装getter和setter2.1.3. case class模拟javabean2.1.4. scala类的构造器2.1.5. 嵌套类...

Baiyabai的博客 815

学会面向对象经典练习题21道

19.主方法中给定数组int[] arr= {20,10,40,30,50,60};如:给定数字3 求出3在数组 int[] arr = {3,4,3,5,7,9};20.定义一个方法,求出给定的数字在给定int型数组中出现的次数,如果一次没有出现则返回0。数组{1,2,3,2} 要查找的数是5 则方法每部会打印 “数组中没有这个数字”如: 数组{1,2,3,2} 要查找的数是2 则方法内部会打印索引值 1 3。要求:枚举值:MON,TUE,WED,THR,FRI,SAT,SUN。

qq_46189388的博客 2303

面向对象练习2

CV在读 169
上一篇: Tomcat Servlet内存马
下一篇: 分享 14 个 Spring MVC 顶级技巧
Java_ttcd
博客等级 码龄4年 198粉丝 190原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值