20145207 2016-2017-2 "Java Programming" 4th Week study Summary

Source: Internet
Author: User
Tags modifiers

I. Inheritance and polymorphism

1. definition of inheritance

In the face of an object, the subclass inherits the parent class, avoids duplicate behavior definitions, but does not use inheritance to avoid repeating defined behavior, which can cause problems with program maintenance.

Program code duplication in the programming is bad signal, multiple classes of duplicate code, one of the design can be considered to improve the way, is to promote the same program to the parent class.

In Java, when inheritance uses the extends keyword, private members are also inherited, except that subclasses cannot be accessed directly and must be accessed through the methods provided by the parent class.

In Java, subclasses can inherit only one parent class, and inheritance has an important relationship, that is, there is a is-a relationship between the subclass and the parent class, and to begin to understand polymorphism, you must first know what kind of object the operation is.

The first one is the RPG software that can run. The second one is the code that is inherited. This is the definition of a swordsman, which is achieved through inheritance. This is the definition of a magician, which is achieved through inheritance.

Although swordsman and magician do not define methods such as GetName (), Getlevel (), and Getblood (), they inherit these methods from role, so they can be used directly in the example. One of the benefits of inheritance is to change the name, level, and blood to other names, as long as you modify Role.java, as long as the subclass that inherits role does not need to be modified. The result of the final program operation:

Although the interface is very low ha, look at the back of things, and then fill in a few interfaces is not difficult. Want to play a game ah, to super-strong kind.

2. polymorphism and is-a

In Java, a subclass can inherit only one parent class, and in addition to the avoidable behavior definition, there is an important relationship between the subclass and the parent class that has a is-a relationship.

The compiler is the grammar checker, to know why the program fragments can be compiled, why not compile, is to use themselves as a compiler, check the syntax logic is correct, the way is from the right of the = sign to the left to read: The right is not the left one.

What is polymorphic? The abstract explanation is that you use a single interface to manipulate multiple types of objects. Check that the polymorphic syntax logic is correct by reading from the right to the left: is the right a left? If it does not compile the failure, if the addition of playing grammar, the compiler will let the program code through compiling, but the consequences, that is, play a failure, execution will throw classcastexception.

3. redefining

After inheriting the parent class, you define the same method deployment as in the parent class, but the execution is different, which is called redefining. The execution is redefined in the subclass because the defined method execution in the parent class is not satisfactory.

4.final Key Words

If the final keyword definition is used before class, then it means that the class is the last one, no more subclasses, and cannot be inherited. When you define a method, you can also qualify the method as final, which means that the method was last defined, that is, the subclass cannot redefine the final method.

· Interface and polymorphism

abstract class : Java can define a method without a method body, the concrete implementation of the method is done by subclasses, the method is called abstract method, contains abstract methods is abstract class. Abstract classes and abstract methods must be decorated with the ABSTRUCT keyword.

interface: The interface defines the behavior but does not define the operation. The member modifiers in the interface are fixed. Classes and interfaces are implementation relationships, and classes can inherit a class and implement multiple interfaces at the same time.

public interface Swimmer{    public abstract void main swim();}

implements: class to manipulate interfaces, using the Implements keyword, when manipulating interfaces, two ways of handling the methods defined in the interface: 1. Method defined in the Operation interface, 2. Mark the method as abstract

There is an inheritance relationship between the class and the class, and the implementation relationship exists between the class and the interface. Inherit with extends, realize with implements

Enumeration constants (for example: public static final int) can often be defined in the interface interface, and in fact, only the enumeration constants of public static final are defined in the interface interface, and must be used = to specify values. Otherwise, a compilation error occurs.

the difference between an abstract class and an interface 1: Abstract classes can only be inherited, and can only be inherited. Interfaces need to be implemented and can be implemented more. 2: Abstract classes can define non-abstract methods, subclasses can directly inherit the use. There are abstract methods in the interface, and subclasses are required to implement them. 3: Abstract class is using the is a relationship. The like a relationship used by the interface. 4: The member modifiers of the abstract class can be customized. The member modifiers in the interface are fixed. All of them are public.

enum: The enum syntax can be used to define enumeration constants, the enum actually defines the class, and the constants enumerated in the enum are actually public static final, and cannot compose the program to instantiate the enumeration type directly because the constructor permission is set to private, can only be instantiated in a class. An enum defines an enumeration constant, but in fact an enum defines a special class that inherits from Java.lang.Enum and generates a Action.class file after compilation, which can be used with this Action declaration type

Problems in teaching materials learning and the solving process

Just start learning to play the first character game code error, in the inheritance, you need to put the full code in the same folder, sometimes just show a part of the code, but the previous role definition to knock up, to ensure the integrity of the code, this way the class will inherit the parent class. Some of the contents of the textbook are very detailed, such as polymorphism and is-a, over and over the different statements may appear to compile the failure of the situation and reasons, this part of the study is also very important, related to the later written things are not flexible, good maintenance problems; There are some less expressions in the book, Places that are not very well understood, such as the instanceof operator, override, and so on, can only be learned through video. At this stage still stay in the process of knocking on the book Code, I hope that I can put forward some more valuable questions.

Problems in code debugging and the resolution process

is declared as a member of the protected, classes in the same package can be accessed directly, and classes in different packages need to be accessed in the inherited subclass. The code does not have a different class of inheritance relationships, compile failed, delete protected compile pass result is 10

Code Hosting

What say what, this week what code also did not knock, Sunday ago do not know can return, first put blog submission (is simply copy before written, you do not want to see copy of the code, I put the code part deleted).

Follow up and fill it up.

20145207 2016-2017-2 "Java Programming" 4th Week study Summary

Related Article

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.