20145207 Java Programming 4th Week of study summary

Source: Internet
Author: User
Tags modifiers

Objective

Again to everyone's favorite preface time, hahaha. I am a person, is more love chatting. The first day of formal school stay here to knock code, I feel great. But it's so boring .... This week's content to tell the truth I am still quite interested, because the example of the book on the game is more exciting. Although it is a simple property configuration, it feels good to be motivated when it comes to gaming. It is not very good to say so, but it is really energetic son! At least play so many games, now know that its property configuration is also excellent. Don't say much nonsense, cut to the chase.

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.

I am still interested in this code of the sword mage above the book, although others know it, but share it.
The first one is the RPG software that can run.

Package ch06;

public class RPG {

public static void Main (string[] args) {

Demoswordsman ();

Demomagician ();

}

static void Demoswordsman () {

Swordsman Swordsman = new Swordsman ();

Swordsman.setname ("Justin");

Swordsman.setlevel (1);

Swordsman.setblood (200);

System.out.printf ("Sword: (%s,%d,%d)%n", Swordsman.getname (), Swordsman.getlevel (), Swordsman.getblood ());

}

static void Demomagician () {

Magician Magician = new Magician ();

Magician.setname ("Monica");

Magician.setlevel (1);

Magician.setblood (200);

System.out.printf ("Magician: (%s,%d,%d)%n", Magician.getname (), Magician.getlevel (), Magician.getblood ());

}

}

The second one is the code that is inherited.

Package ch06;

public class Role {

private String name;

private int level;

private int blood;

public int Getblood () {

return blood;

}

public void Setblood (int blood) {

This.blood = Blood;

}

public int Getlevel () {

return level;

}

public void SetLevel (Int. level) {

This.level = level;

}

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

}

This is the definition of a swordsman, which is achieved through inheritance.

Package ch06;

public class Swordsman extends Role {

public void Fight () {

System.out.println ("sword-wielding attack");

}

}

This is the definition of a magician, which is achieved through inheritance.

Package ch06;

public class Magician extends Role {

public void Fight () {

System.out.println ("Magical Attack");

}

public void Cure () {

SYSTEM.OUT.PRINTLN ("magical Therapy");

}

}

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

For example:

Publicabstract class fish implements Swimmer {protected String name; public Fish this.name = name;} public String getName () {return name;}  @Override public abstract void swim () ;} 

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

PublicClassGame {PublicStaticvoidMainString[] args) {play (action.right); play (action.up);Only the Action instance can be passed in}PublicStaticvoidPlayAction action) {The action argument is declared as the action type, so only the instance of the action is accepted, so you do not need to use the default check, and the compiler does type checking at compile timeswitch (action) {case stop: out.println ("Play Stop Animation"); Break ; Case : out.println ("Play right animation"); Break ; Case  -left: Out.println ("Play-to-animation"); Break ; Case up: out.println ("Play up Animation"); Break ; Case  -down: Out.println ("play down Animation"); Break ;} }} 
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
interface Some {    protected static final int x = 10;}public class Main { public static void main (String[] args){ System.out.println(Some.x); }}

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

Others (sentiment, thinking, etc.)

Do not have any sentiment, tomorrow internship, find the opportunity to follow code, knock code. I love learning, learning makes me happy. ha haha haha. Flag up tomorrow morning. World Goodnight.

20145207 Java Programming 4th Week of 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.