20145235 Java Programming 4th Week of study summary

Source: Internet
Author: User
Tags repetition

Code Hosting

Summarizing and inheriting the contents of textbook learning

• Inheritance: Inheritance basically avoids the repetition of defining common behaviors among multiple classes. What I understand is that in the process of writing a program, there may be some duplication of code, the repetition of the part is defined as a class (the parent Class), in other code (subclass) directly inherit, this way the class can directly use the parent class method, the object of the subclass can also use the parent class method, At the same time, it avoids the problem of repetition of large section code. • Expansion: Keyword Extend

 Public classrpg{ Public Static voidMain (string[] args) {Demoswordsman ();    Demomagician (); }    Static voidDemoswordsman () {Swordsman Swordsman=NewSwordsman (); Swordsman.setname ("Justin"); Swordsman.setlevel (1); Swordsman.setblood (200); System.out.printf ("Sword: (%s.%d,%d)%n", Swordsman.getname (), Swordsman.getlevel (), Swordsman.getblood ()); }    Static voidDemomagician () {Magician Magician=NewMagician (); Magician.setname ("Moinca"); Magician.setlevel (1); Magician.setblood (100); System.out.printf ("Magician: (%s,%d,%d)%n", Magician.getname (), Magician.getlevel (), Magician.getblood ()); }}
public class Magician extends role{public    void Fight ()    {        System.out.println ("magical Attack");    }    public void Cure ()    {        System.out.println ("magical Treatment");       

  

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;    }}

  

Polymorphism and Is-a

Java only supports single inheritance and does not support multiple inheritance. A class can have only one parent class, and there can be no more than one parent class.

Java supports multiple layers of inheritance (inheritance system).

The definition of inheritance requires attention: there is a relationship between the class and the Class ("is a"), and in the previous example, swordsman inherits role, so swordsman is a role.

The code is as follows:

public class rpg2{public    static void Main (string[] args)    {        Swordsman swordsman = new Swordsman ();        Swordsman.setname ("Justin");        Swordsman.setlevel (1);        Swordsman.setblood ($);                Magician Magician = new Magician ();        Magician.setname ("Monica");        Magician.setlevel (1);        Magician.setblood (+);                Showblood (swordsman);        Showblood (magician);            }    static void Showblood (role role)    {        System.out.printf ("%s volume%d%n", Role.getname (), Role.getblood ());}    }

  

Re-define

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.

Abstract methods, abstract classes

Abstract class: A method in Java that does not have a method body can be defined, and the concrete implementation of the method is done by the subclass, which is called an abstract method, and the class containing the abstract method is an abstract class. The origin of abstract methods: multiple objects have the same function, but the specific content of the function is different, then in the extraction process, only the function definition is extracted, the functional body is not extracted, then only the function declaration, the method without functional body is called abstract method. Abstract classes and abstract methods must be decorated with the abstract keyword.

Final keyword

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. Final can be decorated with classes, methods, and variables. The final decorated class cannot be inherited. The final modified method cannot be overridden. A final modified variable is a constant that can only be assigned once. An inner class can only access a final decorated local variable .

Interface definition Behavior

class to manipulate interfaces, the Implements keyword must be used. When operating an interface, there are two ways to define the interface, one is the method defined in the Operation interface, and the other is to mark the method as abstract again. For example:

Public abstract class Fish implements Swimmer {protected string name;public Fish (string name) {this.name = name;} Public String GetName () {return name;} @Overridepublic abstract void Swim ();}

  

For define behavior you can use the interface keyword definition, the methods in the interface cannot be manipulated, are directly labeled abstract, and must be public. The class can manipulate more than two classes, which means more than two behaviors. A class can inherit a class at the same time and manipulate some interfaces, which can inherit from another interface, that is, inherit the parent interface behavior, and then define the behavior in the sub-interfaces in additional.

public class Human implements Swimmer {private string name;public Human (String name) {this.name=name;} Public String GetName () {return name;} @Overridepublic void swim (); {System.out.printf ("Human%s swimming%n", name);}}

  

Interface Syntax details

Use interface to define the appearance of an abstract behavior, which is declared as public abstract, and is not required and cannot be manipulated. For convenience, the public abstract can also be omitted, and the compiler will assist in completing the process. You can use interface enumeration constants, which can only be defined as public static final, or you can omit public static final for convenience.

Interfaces can inherit other interfaces, or they can inherit more than two interfaces at the same time, and also use the extends keyword.

The enum syntax can be used to define enumeration constants, which actually define the class, and the constants enumerated in the enum are actually public static final, and cannot be written to instantiate the enumeration type directly because the constructor permission is set to private and only in the class can be instantiated.

Problems in teaching materials learning and the solving process

Other (sentiment, thinking, etc., optional)

At the beginning of this week's mission, one saw what "magician", "Swordsman" ... Feel good surprised, learn Java so short time, I will be programming game! With this surprise I began to study this week, however, according to my current level of this rookie, programming game is a day implausible ah. This week all of a sudden feel good time, I want to do too much, want to learn Java, also want to do their own things, prepare computer two class, learn English, practice words, read books, sports, to feel the smell of spring. However, many do not have the time to do it. This week I feel the Java Learning Task rhythm is fast ah, in fact, some of the previous knowledge has not been properly digested immediately followed by a new week to learn. I hope this week will be better.

20145235 Java Programming 4th Week of study summary

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.