20145239 Du Wenshu "Java Programming" 4th Week study Summary

Source: Internet
Author: User

20145239 "Java Program Design" The 4th Week study summary textbook Learning content Summary

The sixth chapter:

inheritance: avoid repeating defined common behaviors among multiple classes. That is, when the same properties and behaviors exist in multiple classes, the content is extracted into a single class, so that multiple classes do not need to define these properties and behaviors, as long as they inherit a separate class. We also refer to the class that is extracted separately as the parent class, and the other multiple classes are called subclasses.

inherited keyword:extends

For example in the book: Role is a parent class, which is the basic property of both swordsman and magician, it is important to note that we say that inheritance is a is-a relationship, Swordsman inherits role, so swordsman is a role.

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

The swordsman inherits all of the properties of role and has its own unique properties:

public class Swordsman extends role{public    void Fight ()    {        System.out.println ("sword Attack");}    }

 The final code:

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 ($);        System.out.printf ("Swordsmen: (%s,%d,%d)%n", Swordsman.getname (),                swordsman.getlevel (), Swordsman.getblood ());    }    static void Demomagician ()    {        Magician Magician = new Magician ();        Magician.setname ("Moinca");        Magician.setlevel (1);        Magician.setblood (+);        System.out.printf ("Magician: (%s,%d,%d)%n", Magician.getname (),                magician.getlevel (), Magician.getblood ());}    }

  *tip:Multi-layer inheritance is also supported in Java:

Class A{}class B extends A{}class C extends b{}

It's like the relationship between the father and the son.

Polymorphism : The interpretation of a polymorphic literal is a variety of existence patterns of a certain class of things, and the abstract explanation is that using a single interface to manipulate multiple types of objects. If the number of roles is large, as long as they inherit the same class (role) can use polymorphic methods instead of overloading, improve the maintainability of the program.

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 ("Moinca");        Magician.setlevel (1);        Magician.setblood (+);        Showblood (swordsman);   Swordsman is a role        showblood (magician);    }    static void Showblood (role role)   //declared as role type    {        System.out.printf ("%s Blood%d%n", Role.getname (), Role.getblood ());}    }

  

Abstract Method: If there is no program code operation in a method block, you can use abstract to mark the method.

Abstract class: If there is a method in the class that has no action and is marked abstract, it means that the class definition is incomplete and therefore cannot be used to generate an instance. In Java, classes that contain abstract methods must be marked abstract in front of class, indicating that this is an incomplete definition of a class.

Public abstract class Guessgame {public        void Go () {            int number= (int) (Math.random () *10);            int guess;            do {                print ("Enter Number:");                Guess=nextint ();            } while (Guess!=number);            println ("guessed");        }            public void println (String text) {            print (text+ "\ n");        }            public abstract void print (String text);        public abstract int Nextint ();    }

  

Import Java.util.Scanner;        public class Consolegame extends Guessgame {        private Scanner scanner=new Scanner (system.in);            @Override public        void print (String text) {            System.out.print (text);        }            @Override public        void println (String text) {            System.out.println (text);        }            @Override public        int Nextint () {            return scanner.nextint ();        }    }
public class Guess {public        static void Main (string[] args) {            guessgame game=new consolegame ();            Game.go ();        }    }

  

inheritance Syntax Details: members declared as protected, classes in the same package can be accessed directly, and classes in different packages can be accessed directly from the inherited subclass.

Garbage Collection: creating objects takes up memory, and if an object is no longer available in the program execution process, the object is just a waste of memory. The JVM has a garbage collection mechanism, and the collected garbage objects occupy a memory space that is freed by the garbage collector.

Garbage object: An object that cannot be referenced by a variable in the execution process.

The seventh chapter:

interface: to focus on the differences between understanding and inheritance, Java interfaces support multiple inheritance, and the operation interface represents "owning behavior", but not "being a" relationship.

The methods defined in the interface are handled in two ways: 1. The method defined in the Operation interface. 2. The method is again marked as abstract.

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

interface Keyword:implements

Running an instance:

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 an interface enumeration constant, which can only be defined as public static final. For convenience, you can also omit public static final.
    • 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

While reading the textbook, I found a key word that I didn't understand: @Override. The following error occurred while knocking the code:

I then found the answer in a post in the blog park:

"@Override is JAVA5 metadata, automatically added to a logo, tell you that the following method is inherited from the parent class/interface, you need to rewrite it once, so that you can easily read, not afraid to forget.

The token is used to enhance the check of the program at compile time, and if the method is not a method that overrides the parent class, the compiler will report an error at compile time. ”

You can click this post for details. Http://www.cnblogs.com/hnrainll/archive/2011/10/17/2215138.html

Problems in code debugging and the resolution process

In the seventh chapter, many of the code in the textbook is purely class and cannot be executed. I found in idea that I could not run the code, and thought that the code is a problem, in fact, a closer look only to find that the code is not the main function, so can not run.

This week's code hosting

Other (sentiment, thinking, etc., optional)

The difficulty of speaking true Java learning is becoming more and more difficult, and I feel very labored. Maybe last week's Four or Five chapter content is not solid enough, next week's learning task will come, in fact, watching video plus reading plus code and write blog task volume is very heavy. Perhaps I have not fully adapted to this self-learning model. This week's content I was most impressed by the difference between the inheritance and interface, the book is very vivid examples, fish can swim, sharks are fish, clown fish is fish, so they are inheritance relationship, but people can swim, but people and fish there is no inheritance relationship, but the human and fish between the connection can be expressed by the interface. While this is understood, I cannot relate to how much of a difference these two definitions can have in a particular program. In short, to continue to learn or to fill up the knowledge still a lot, I hope I can persist, do not end up in the course of the people to become mixed. The human path is the vicissitudes of the world Ah!

Learning progress Bar /Cumulative) new/cumulative)
lines of code (newBlog volume (Learning time (new/cumulative) Important growth
Goal 5000 rows 30 Articles 400 hours
First week 100/100 1/2 12/15
Second week 200/300 1/2 15/15
Third week 300/300 1/2 20/20
Week Four 500/500 1/2 25/25

Resources
    • Java Learning Notes (8th Edition)
    • Java Learning Note (8th Edition) Learning Guide

20145239 Du Wenshu 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.