Inheritance of Class 2 in Java experiment

Source: Internet
Author: User

Inheritance of the Experiment Class 2
First, the purpose of the experiment
Mastering the implementation of object-oriented inheritance in Java, mastering the use of super keywords, and understanding the role of abstract classes and abstract methods.
Ii. contents of the experiment
1. Program Understanding:
1) Inheritance of classes

2) The 4th chapter after the program 1th question

Class Student { PublicString name; Public intAge Public Student(String name,intAge) { This. name=name; This. age=age; } Public void Show() {System. out. println ("Name:"+name+"Age:"+age); }}class Undergraduate extends student{ PublicString degree; Public Undergraduate(String name,intAge,string degree) {Super (name, age); This. Degree=degree; } Public void Show() {System. out. println ("Name:"+name+"Age:"+age+"degree:"+degree); }} Public classtest01{ Public Static void Main(string[] args) {Student Student =NewStudent ("Zhangsan", -);        Student.show (); Undergraduate undergraduate =NewUndergraduate ("Lisi", -,"Bechalor");    Undergraduate.show (); }

2. Programming Questions:
(1) Please design the class according to the following requirements. (Procedure 1)
Requirements are as follows:
1) Design Animal class. A member variable animalname is defined in a class that represents the name of the animal, the variable animalname is the string type, and the default initial value. A member method, shout (), is defined in this class to indicate the behavior of the animal to emit a call, which can output a call from an animal named Animalname, such as an output "Persian cat beeps".
2) Design Cat class. Use the keyword extends to inherit the animal class. The Cat class defines a construction method of the public type that has a string-type parameter, catname, that assigns a value to the Animalname member variable in the Cat class that inherits from the animal class.
3) design a test class. The class name is Extendstest, define a String type name variable in the main method, assign it a "Persian cat", and then use a parametric construct to create an object of the Cat class and invoke the shout () method using the object's reference variable.

(2) The design of the parent class and a subclass, within the subclass through the Super keyword to call the parent class member variables and member methods, in the child class construction method using the Super keyword call the construction method of the parent class, in the test class to verify. (Procedure 2)

(3) Create an inheritance hierarchy for the planar graphic abstract class (Planegraphic) and its subclass triangles (Triangle), Circle (circle), Rectangle (Rectangle). In abstract class Planegraphic, an abstract method for calculating the perimeter and area of a graph is provided, and an abstract method is implemented in each subclass to calculate the corresponding perimeter and area based on different types of planar graphs. Tip: The triangle area is calculated with the Helen formula: P is the half perimeter of the triangle. (Procedure 3)

(4) Please design a outer class according to the following requirements. (Procedure 4)
Requirements are as follows:
1) define an external class outer, and define a static inner class inner in the class.
2) define a static variable Staticfield and a static method Staticmethod () in the inner class, and set the value of the variable to "static variable of static inner class", which outputs "static method of static inner class".
3) define a test class, output the value of Staticfield in the main () method, and call the static method Staticmethod ().

(5) familiar with Eclipse development tools.
Learn the 11th chapter of your textbook, familiarize yourself with Eclipse for program debugging (page 418), and learn about importing and exporting jar files (422 pages).

Third, experimental results and analysis
Description: Please give the operation result of each program, and do the necessary analysis.
(1) Program 1 operation result

Analysis: The Cat class inherits the animal class through the extends keyword, so that the cat class is a subclass of the animal class. From the running results it is not difficult to see that although the subclass does not define the Animalname attribute and the Shout () method, but it can access the two members, this means that when the subclass inherits the parent class, it automatically owns all the members of the parent class, and the cat class defines the parameter constructor. Using a parametric construct in the main method of the test class to create a Cat class object, run the result

(2) Program 2 operation result

Analysis: Defines a child class that inherits the Father class and overrides the Eat () method of the Father class, using "Super.eat ()" in the Eat () method of the subclass children to invoke the overridden method of the parent class, using "in the Introfather () method" Super.age "accesses the member variables of the parent class. As you can see from the running result, subclasses successfully access the member variables and member methods of the parent class through the Super keyword. When instantiating a child object, it is certain that the child class's constructor is called, and it can be seen from the running result that the constructor method of the parent class is called when the child class's constructor is called.

(3) Program 3 operation result

Analysis: From the running results, it can be seen that the subclass implements the abstract object of the parent class, can be instantiated normally, and invoke the method by instantiating the object. Abstract class is actually a method of constraining subclasses named, in the abstract parent class to define abstract methods, implemented in subclasses, different parameters are implemented through member variables.

(4) Program 4 operation result

Analysis: The inner class inner is decorated with the static keyword, which is a statically intrinsic class that is instantiated in a different way from the instantiation of a non-static member's inner class, in the static inner class CAs, which defines static members, while static members are not allowed in non-static inner classes.

Iv. source code of the experiment
Description: Please copy and paste the source code of each program here.
(1) Program 1 source code

//define Animal class class Animal{String Animalname;//Define how animals are calledvoid Shout () {System.out.println (animalname+"Make a scream."); }}//define Cat Class inheritance animal class class Cat extends Animal{    //Defines a method for constructing a public type in a class     PublicCat (String catname) {animalname=catname; }}//define test class Public  class Extendstest {     Public Staticvoid Main (string[] args) {String name="Persian cat";//Define a String type name variable and assign it a value of "Persian cat",Cat cat=NewCat (name);//Using a parametric construct to create an object of cat classCat.shout ();//Use the object's reference variable to call the Shout () method. }}

(2) Program 2 source code

//define Father class class Father{    intAge= -; String food="Little Bear Cookie";//Define father class with parameter constructor     PublicFather (String name) {System.out.println ("My name is called"+name); }//define how you like to eat    voidEat () {System.out.println ("I like to eat."+food); }}//define child class class  child extends Father{    //Use the Super keyword in the constructor method of a subclass to invoke the constructor of the parent class,     PublicChild () {Super("Crayon Little New"); }//In subclasses, the Super keyword is used to invoke the Member method of the parent class.    voidEat () {Super. Eat (); }//In subclass to invoke the member variable of the parent class through the Super keyword    voidIntrofather () {System.out.println ("My father this year."+age+"years old."); }}//define test class Public  class Example2 {     Public Static voidMain (string[] args) {Child child=NewChild ();//Create an instance object of the child classChild.eat ();//Call the Eat method of the child classChild.introfather ();//Call the Introfather method of the child class}}

(3) Program 3 source code

Import Java.util.Scanner;//define abstract class PlanegraphicAbstract  class planegraphic{    DoubleArea//define abstract methods Getarea    Abstract voidGetarea ();}//Define triangle class to inherit abstract class Planegraphic class Triangle extends planegraphic{    DoubleA,b,c;//define triangle class with parameter constructor     PublicTriangle (DoubleADoubleBDoublec) { This. a=a; This. b=b; This. c=c; }//Implement abstract Method Getarea ()    voidGetarea () {Doublep= (A+B+C)/2;DoubleArea=math.sqrt (p* (p-a) * (p-b) * (p-c)); System.out.println ("The area of the triangle is:"+area); }}//define Circle Class inheritance abstract class Planegraphic class Circle extends planegraphic{    DoubleR//define the Circle class with a parameter constructor     PublicCircle (DoubleR) { This. r=r; }//Implement abstract Method Getarea ()    voidGetarea () {DoubleArea=math.pi*r*r; System.out.println ("The area of the circle is:"+area); }}//define rectangle class to inherit abstract class Planegraphic class Rectangle extends planegraphic{    Doublex, y;//define rectangle class with parameter constructor     PublicRectangle (DoubleXDoubleY) { This. x=x; This. y=y; }//Implement abstract Method Getarea ()    voidGetarea () {DoubleArea=x*y; System.out.println ("The area of the rectangle is:"+area); }}//define test class Public  class Example3 {     Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter the side length of the triangle:");DoubleA1=sc.nextdouble ();DoubleB1=sc.nextdouble ();DoubleC1=sc.nextdouble (); Triangle t=NewTriangle (A1,B1,C1);//Instantiate Triangle ObjectT.getarea (); System.out.println ("Please enter the radius of the circle:");DoubleR1=sc.nextdouble (); Circle c=NewCircle (R1);//Instantiate Circle ObjectC.getarea (); System.out.println ("Please enter the side length of the rectangle:");DoubleX1=sc.nextdouble ();DoubleY1=sc.nextdouble (); Rectangle re=NewRectangle (X1,Y1);//Instantiate Rectangle objectRe.getarea (); }}

(4) Program 4 source code

//define an external classClass outer{//The following code defines a static inner class    StaticClass inner{StaticString staticfield=Static variables for static internal classes;Static voidStaticmethod () {System. out. println (Static methods for static internal classes); }    }}//define test class Public classExample4 { Public Static void Main(string[] args) {Outer.Inner inner=NewOuter.Inner ();//Create inner class objectSystem. out. println (Inner.staticfield);    Inner.staticmethod (); }}

Inheritance of Class 2 in Java experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.