JAVA exercises (1)-I am a singer

Hello everyone!
I’m Xiao Huang, I’m glad to meet you again!
The update today is:


Creation time : October 23, 2020
Software version : eclipse 2020-06 (4.16.0)


table of Contents:

1. Title:

1. Requirements for this experiment: Experience the definition and use of abstract classes with different singers singing different songs as the background.

  • 1-1. Business description :
    1-1.1. Singer is a collective term for those who have the ability to sing.
    1-1.2. Specific singers have different styles and types, such as popular and ethnic ones;
    1-1.3. This experiment will simulate different singers singing different songs as the background to experience the definition and use of abstract types.
    1-2. Create a project and configure the environment:
    1-2.1. Restrictions 1. Create an experimental project, named: SE_JAVA_EXP_E035P;
    1-2.2. Restrictions 2. Create a package, named: cn.campsg.java.experiment;
    1-2.3 . Restriction 3. Create a package and name it: cn.campsg.java.experiment.entity.
    1-3. Create an abstract class for singers:
    1-3.1. Restrictions 1. Create an abstract class in the cn.campsg.java.experiment.entity package: AbstractSinger.
    1-3.2. Add "Introduction to Occupation" method to the AbstractSinger class:
    1) The display content is: "Hello, I am a singer.";
    2) The requirements for the definition of "Introduction to Occupation" method are as follows:
    Insert picture description here
    1-3.3. Add to the AbstractSinger class The abstract method of "singing", the method definition requirements are as follows:
    Insert picture description here
    1-4. Create pop singer class
    1-4.1. Restrictions 1. Create pop singer class in cn.campsg.java.experiment.entity package: PopSinger
    1-4.2. Use The PopSinger class inherits the AbstractSinger abstract class.
    1-4.3. Implement the sing abstract method defined in the AbstractSinger class in the PopSinger class. The implementation requirements are as follows:
    1) Output "I am singing pop music." in the method body to the console.
    1-5. Simulate a singer singing:
    1 -5.1. Restrictions 1. Create a main class in the cn.campsg.java.experiment package: MainClass;
    1-5.2. Create an entry main method for MainClass: main;
    1-5.3. In main, instantiate one with the pop singer class Singer object.
    1-5.4. In main, call the introduce method and sing method of the singer object.
    1-5.5. View and verify the output result in the console.

  • 2. Realization ideas:

    2-1. Create a project and configure the environment.
    2-2. Create a singer abstract class:
    2-2.1. Restrictions 1. Create an abstract class in the cn.campsg.java.experiment.entity package: AbstractSinger;
    2-2.2. Add an "introduction to occupation" method to the AbstractSinger class introduce:
    1) This method is an implemented method of the abstract class for direct use by subclasses;
    2) The logic of this method is implemented as the display content: "Hello, I am a singer.";
    3) This method is defined as follows:
    Insert picture description here
    2-2.3. For AbstractSinger The abstract method sing to add "singing" to the class:
    1) This method is implemented by a concrete subclass, and only the method form is defined in the abstract class:
    Insert picture description here
    2-3. Create pop singer class
    2-3.1. Restrictions 1. In cn.campsg.java. Create a pop singer class in the experiment.entity package: PopSinger 2-3.2
    . Add a parameterless constructor to the PopSinger class.
    2-3.3. Make the PopSinger class inherit the AbstractSinger abstract class.
    Insert picture description here2-3.4. Implement the sing abstract method defined in the parent class in the PopSinger class. The implementation requirements are as follows:
    1) Output "I sing pop music." in the method body to the console.
    Insert picture description here
    2-4. Simulate singer singing
    2- 4.1. Restrictions 1. Create a main class in the cn.campsg.java.experiment package: MainClass;
    2-4.2. Create an entry main method for MainClass: main;
    2-4.3. In main, instantiate a singer object with the pop singer class.
    2-4.4. In main, call the introduce method and sing method of the singer object.
    2-4.5. View and verify the output result in the console.
    Insert picture description here

  • 3. Verification and testing:

    3-1. Locate the main class MainClass in the project.
    3-2. Right-click the MainClass class and select in turn: Run As->Java Application.
    3-3. Run the program to check whether the output result meets expectations.

  • 4. Complete effect preview:

Insert picture description here

2. Code:

  • eclipse project directory:

Insert picture description here

  • 包名 :cn.campsg.java.experiment
  • Class name: MainClass
package cn.campsg.java.experiment;

import cn.campsg.java.experiment.entity.AbstractSinger;
import cn.campsg.java.experiment.entity.PopSinger;

public class MainClass {
    
    

	public static void main(String[] args) {
    
    
		
		AbstractSinger s = new PopSinger();
		s.introduce();
		s.sing();
	}

}

  • 包名 :cn.campsg.java.experiment.entity
  • Class name: AbstractSinger
package cn.campsg.java.experiment.entity;

public abstract class AbstractSinger {
    
    
	public void introduce() {
    
    
		System.out.println("Hello,我是歌手。");
	}

	public abstract void sing() ;
}

  • 包名 :cn.campsg.java.experiment.entity
  • Class name: PopSinger
package cn.campsg.java.experiment.entity;

public class PopSinger extends AbstractSinger{
    
    
	public void sing() {
    
    
		System.out.println("我是唱流行乐的。");
	}
}


Friends passing by, if you think you can learn something , please give a thumbs up and let's go. Welcome to the big guys passing by to comment, correct mistakes, and welcome friends who have questions to comment, leave messages, and send private messages.

The attention of every small partner is my motivation to update my blog! ! !
Please search WeChat for [ Zaixiaxiaohuang ] article updates will be read as soon as possible!
Insert picture description here

Grasp the present, look to the future, come on!


Due to the limited level, there will inevitably be some shortcomings in the writing. I urge everyone to take your advice!

Guess you like

Origin blog.csdn.net/weixin_44519789/article/details/109249515