8.8 Inheritance self-testing

Programming problem

1, the establishment of a human (Person) class and student (Student), functional requirements are as follows:
(1) the Person of the type of data includes four private member name, addr, sex, age, respectively, string, string, string and plastic. Indicate the name, address, gender and age. A four parameter constructor, a two-argument constructor method, a constructor with no arguments, a method of displaying four output attributes.

package com.lxh.eightchapter;

public class Person185 {
       private String name;
       private String addr;
       private String sex;
       private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person185() {
	}
	public Person185(String name, String addr) {
		this("李黑","陕西","男",20);
	}
	public Person185(String name, String addr, String sex, int age) {
		this.name = name;
		this.addr = addr;
		this.sex = sex;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person185 [name=" + name + ", addr=" + addr + ", sex=" + sex + ", age=" + age + "]";
	}
	
	
	
       
}

(2) Student class inherits from the Person class, and to increase the membership of math, english deposit math and English scores. A six parameter constructor method and a two-argument constructor, a constructor parameter and output six attributes no.

package com.lxh.eightchapter;

public class Student185 extends Person185 {
       private int math;
       private int english;
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
	public int getEnglish() {
		return english;
	}
	public void setEnglish(int english) {
		this.english = english;
	}
	public Student185() {
	}
	public Student185(String name,String addr) {
		super(name,addr);
	}
	
	public Student185(String name,String addr,String sex,int age,int math,int english) {
		super(name,addr);
		this.math=math;
		this.english=english;
	}
	@Override
	public String toString() {
		return "["+super.toString()+"math=" + math + ", english=" + english + "]";
	}
	
	
       
}

package com.lxh.eightchapter;

public class Java185 {
    public static void main(String[] args) {
    	Person185 per=new Person185("李黑","陕西");
    	System.out.println(per.toString());
    	Student185 stu=new Student185("张三","陕西","男",20,80,60);
    	System.out.println(stu.toString());
	}
}

Results of the

Person185 [name=李黑, addr=陕西, sex=男, age=20]
[Person185 [name=李黑, addr=陕西, sex=男, age=20]math=80, english=60]

2, the definition of the class of employees, with name, age, sex, property, and has a constructor method and display data. Defined management classes, inheritance class employees, and has its own attributes and salary job. Staff defined classes, inheritance class employees, and has its own attributes and salary department.

package com.lxh.eightchapter;

public class Staff {
      private String name;
      private int age;
      private String sex;
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Staff() {
	}
	public Staff(String name, int age, String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Staff [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
      
}

package com.lxh.eightchapter;

public class Admin extends Staff {
       private String duty;
       private double salary;
	public String getDuty() {
		return duty;
	}
	public void setDuty(String duty) {
		this.duty = duty;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public Admin() {

	}
	public Admin(String name, int age, String sex,String duty, double salary) {
        super(name,age,sex);
		this.duty = duty;
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "["+super.toString()+"duty=" + duty + ", salary=" + salary + "]";
	}
	
	
       
}

package com.lxh.eightchapter;

public class Clerk extends Staff{
       private String section;
       private double mot;
	public String getSection() {
		return section;
	}
	public void setSection(String section) {
		this.section = section;
	}
	public double getMot() {
		return mot;
	}
	public void setMot(double mot) {
		this.mot = mot;
	}
	public Clerk() {
		
	}
	public Clerk(String name, int age, String sex,String section, double mot) {
		super(name,age,sex);
		this.section = section;
		this.mot = mot;
	}
	@Override
	public String toString() {
		return "["+super.toString()+"section=" + section + ", mot=" + mot + "]";
	}
	
       
}

package com.lxh.eightchapter;

public class java2185 {
       public static void main(String[] args) {
    	Admin pera=new Admin("李世",50,"男","经理",50000.20);
		Clerk per=new Clerk("李雷",20,"男","人事部",500.2);
		System.out.println(pera.toString());
		System.out.println(per.toString());
	}
}

Results of the

[Staff [name=李世, age=50, sex=男]duty=经理, salary=50000.2]
[Staff [name=李雷, age=20, sex=男]section=人事部, mot=500.2]

3, programming, statistics of the number of strings want you to konw one thing in the letter n and o?

public class java3185 {
       public static void main(String[] args) {
		String str="want you to know one thing";
		char rest []=str.toCharArray();
		int countn=0;
		int counto=0;
		for(int x=0;x<rest.length;x++) {
			int mata=rest[x];
			if(mata==110) {
				countn++;
			}
			if(mata==111) {
				counto++;
			}
		}
		System.out.println("n的个数为:"+countn);
		System.out.println("0的个数为:"+counto);
	}
}

Results of the

n的个数为:4
0的个数为:4

4, the establishment of a operation class shaping the array (Array) can be achieved, the class allows the operator to array size is specified by an external dynamic, while the Array class need to provide an array of the following treatment: increased data (if the data is full is not increase), can achieve capacity expansion of the array, the array element entirety. Again derived on the basis of two sub-classes after completion.

package com.lxh.eightchapter;

public class Array {
       private int date[];  //整形数组
       private int foot;   //数组索引控制
	public int[] getDate() {
		return date;
	}
	public void setDate(int[] date) {
		this.date = date;
	}
	public int getFoot() {
		return foot;
	}
	public void setFoot(int foot) {
		this.foot = foot;
	}
       public Array(int len) {
    	   if(len>0) {
    		   this.date=new int[len];//开辟空间
    	   }else {
    		   this.date=new int[1];//开辟一个空间
    	   }
       }
       
       //实现数组容量扩充,给出的是扩充大小。实际大小:已有大小+扩充大小
       public void increment(int num) {
    	   int [] newdata=new int[this.date.length+num];
    	   System.arraycopy(this.date, 0, newdata, 0, this.date.length);
    	   this.date=newdata;//改变数组引用
    	   
       }
       public boolean add(int num) {     //数据增加
    	   if(this.foot<this.date.length) {//有位置
    		   this.date[this.foot++]=num;
    		   return true;
    	   }
    	   return false;
       }
       
}

在这里插入代码片
  • Array sorting categories: Returns the data must be sorted results.
package com.lxh.eightchapter;

public class SortArray extends Array {

	public SortArray(int len) { //定义排序子类
		super(len);
	}
    public int[] getDate() {
    	java.util.Arrays.sort(super.getDate());
    	return super.getDate();
    }
}

package com.lxh.eightchapter;

public class DemoArray {
       public static void main(String[] args) {
		SortArray da=new SortArray (5);
		System.out.println(da.add(5));
		System.out.println(da.add(20));
		System.out.println(da.add(52));
		System.out.println(da.add(51));
		System.out.println(da.add(22));
		
		da.increment(3);
		System.out.println(da.add(22));
		System.out.println(da.add(2));
		System.out.println(da.add(2));
		int [] re=da.getDate();
		for(int temp:re) {
			System.out.println(temp+"、");
		}
		
	}
}

Results of the

2、
2、
5、
20、
22、
22、
51、
52、

  • Reverse arrays categories: end to end may be implemented exchanging content.
package com.lxh.eightchapter;

public class RerverArray  extends Array{  //定义反转子类

	public RerverArray(int len) {
		super(len);
		
	}
	 public int[] getDate() {
	    	int center=super.getDate().length/2;
	    	int head=0;
	    	int tail=super.getDate().length-1;
	    	for(int x=0;x<center;x++) {
	    		int temp=super.getDate()[head];
	    		super.getDate()[head]=super.getDate()[tail];
	    		super.getDate()[tail]=temp;
	    		head++;
	    		tail--;
	    	}
	    		return super.getDate();
	    }

}

package com.lxh.eightchapter;

public class DemoArray {
       public static void main(String[] args) {
        RerverArray da=new RerverArray (5);
		System.out.println(da.add(5));
		System.out.println(da.add(20));
		System.out.println(da.add(52));
		System.out.println(da.add(51));
		System.out.println(da.add(22));
		
		da.increment(3);
		System.out.println(da.add(22));
		System.out.println(da.add(2));
		System.out.println(da.add(2));
		int [] re=da.getDate();
		for(int temp:re) {
			System.out.println(temp+"、");
		}
		
	}
}

Results of the

2、
2、
22、
22、
51、
52、
20、
5、

Fill in the blank

1, Java via extends inheritance keyword.
2, Object class is the class of the parent class, the class is determined whether the two objects are equal is to public Boolean the equals (Object obj) , the method is a complete information object is achieved public String toString () .
3, when the subclass defined in the parent class method with the same name and type and number of parameters, return type is the same, said method subclass override the parent class method, the default subclass of this class have override method using the parent class method of the same name, you must use the super keyword description.
4, when a member variable of the subclass definition member variable with the same name as the parent class, subclass said cover member variable parent class, a subclass of the default of this class attributes. Use the same name as the parent class member variables, you must use super Keyword.
5, if the child class defines a constructor, when you create a subclass object first calls the default parent class constructor without parameters , then this class constructor.
6, Java offers three built-in Annotation is: @Override , @Deprecated ,@SuppressWarnings

Multiple Choice

1, a plurality of the same name for the class definition, but the practice of different types or the number of parameters is called method A .
A, B method overloading, override method C, D inherited methods, reuse method
2, there are two classes A, B, B represents the following description is inherited from the A D .
A, BB the extends class A, class B the implements A
C, D the implements class A, class B A the extends
. 3, the following description of the subclass constructor call parent class is the correct C .
A, sub-class defines its own constructor, we would not call the constructor of the parent class.
B, sub-class parent class constructor must be called with a parameter of the super keyword.
C, if the constructor by a subclass constructor does not call the parent super class, then subclass will first call the parent class constructor parameter-free, after then call their own subclass constructor.
D, when creating a subclass object to call their own subclass constructor, so then call the constructor of the parent class.
4, assuming that X is the class parent class Y, the following declaration statement object x is incorrect D .
A, X-X = new new X-(); B, X-X = new new the Y ();
C, the Y X = new new the Y (); D, the Y X = new new X-();
. 5, compile and run the following program, the results of B .
public class A {
static void main public (String args []) {
B B B = new new ();
b.test ();
}
void Test () {
of System.out.print ( "A");
}
}
class B {A the extends
void Test () {
super.test ();
System.out.println ( "B");
}
}
A, B a compilation error, the code can be compiled to run, and outputs the result: AB
C, the code can be compiled to run, but no output D, compile without error, but will generate a runtime exception
6, compile and run the following program, the result is a .
class A {public
public static void main (String args []) {
B B B = new new ();
b.test ();
}
public void Test () {
of System.out.print ( "A");
}
}
class the extends A {B
void Test () {
super.test ();
System.out.println ( "B");
}
}
A, a compilation error, because the cover for class B class A test (time), which reduces the level of access control.
B, code can be compiled to run, and outputs the result: AB
C, the code can be compiled to run, but no output
D, code can be compiled to run, and outputs the result: A
. 7, there are two classes A, B, B represents the following description a is inherited from D .
A, BB the extends class A, class B the implements A
C, D the implements class A, class B A the extends
. 8, the following A method as defined modifiers subclasses not overwritten.
A, final B, abstract D, static D, interface

True or False

1, a class that is not a subclass is the parent class. ( )
2, members of the subclass can only inherit the parent class, but can not modify the parent class members. (×)
3, the Java language supports only single inheritance, do not support multiple inheritance. (√)
4, a subclass can inherit all the members of the parent class. (√)

Short answer

1, illustrates a process example of the subclass object.
A: When a new instance of the keyword subclasses of objects, by default no-argument constructor calls the parent class method, the parent class object is instantiated, then will call the constructor of a subclass of a subclass object is instantiated.

2, outlined the difference between this and super keywords.
A: this can be called and super class attributes, methods, construction methods, but that the present call this type of operation, and is called the father super operated by subclasses.

3, with the difference overloaded briefly overwriting method.
A: Method overloading occurs in a class, the same method name, and the number of parameters of different types, not limited by the authority. Overwriting occurs in the inheritance, the subclass defines the operations that occur when the same parent class defines a method name, type and number of parameters, the method returns the same value type, subclass override the superclass when the method is overridden method can not have more stringent than the parent access.

4, on the basis of existing class to derive new classes What are the benefits?
A: The expansion of an existing class of function, and function of the existing methods of using the method override expansion.

5, how district molecular classes and parent classes? Subclasses can inherit those elements of the parent class?
A: subclasses extends inherit the parent class, subclass can inherit the entire contents of the parent class, but for the private operation belong implicitly inherited, rather than explicit inheritance belonging to private operation.

6. What is a multi-state? Way to achieve both of those states?
A: The polymorphism is divided into two areas:
the polymorphism · approach: overloading and override
| - Overloaded: The same method name, you can perform different functions depending on the type and number of parameters; |
- override: the same method, according to different subclasses of operation, different functions performed.
· Polymorphism objects: conversion of parent-child class object.
| - upcast: subclass object in the parent class object, in the format: parent = parent class object subclass instance, automatically; |
- downcast: parent object becomes subclass object, format: subclass subclass object = (subclass) instance of the parent class, mandatory;

7, JAVA SE brief role in the three built-in Annotation.
· @Override: accurate method override;
· @Deprecated: a structure (classes, methods, properties) are no longer recommend the use of a user;
· @SuppressWarnings: suppress warning messages when compiling generated.

Published 162 original articles · won praise 9 · views 3111

Guess you like

Origin blog.csdn.net/ll_j_21/article/details/104447883