The question of the Nottingham Tower implemented by the idea of Java object-oriented

Source: Internet
Author: User

 Packagehanoi.com; Public classDisc {PrivateString name; Private intLevel ;  PublicDisc () {name= "Disc"; level= 0; }     PublicDisc (String name,intLevel ) {         This. Name =name;  This. Level =Level ; }         PublicString GetName () {returnname; }     Public intGetlevel () {returnLevel ; } @Override PublicString toString () {return"Name:" + This. name + "; Level:" + This. Level; }}

 Packagehanoi.com;Importjava.util.LinkedList;Importjava.util.List; Public classPost {//used to install disc .    Privatelist<disc> lists =NewLinkedlist<disc>(); PrivateString name; PublicString GetName () {returnname; }     PublicPost (String name) { This. Name =name; }     Public voidAdd (Disc Disc) {if(Disc = =NULL)return;  This. Lists.add (0, disc); }     Public voidAdd (list<disc>discs) {        if(Discs = =NULL|| Discs.size () < 1) {            return; } lists.addall (0, discs); }    //the plates in the current column are moved to Targetpost with Middlepost.     Public voidMove (post middlepost, post Targetpost,postmove postmove) { This. Move ( This. Lists.size (), Middlepost, Targetpost, Postmove); }    Private voidMoveintTotal , post post1, post Post2,postmove postmove) {        if(Total <= 0)return;  This. Move (Total-1, Post2, Post1,postmove); Disc Disc= This. Lists.remove (0); if(Postmove! =NULL) {postmove.action ( This, Post1,post2, disc);                } post2.add (disc); Post1.move ( Total-1, This, Post2,postmove); } @Override PublicString toString () {return"Post [lists=" + Lists + ", name=" + name + "]"; }}
 Package hanoi.com;  Public Interface Postmove {    publicvoid  action (post Scrpost,post middlepost, post targetpost, Disc disc);}
 Packagehanoi.com.test;ImportHanoi.com.Disc;ImportHanoi.com.Post;ImportHanoi.com.PostMove; Public classMain { Public Static voidMain (string[] args) {Post post1=NewPost ("A"); Post Post2=NewPost ("B"); Post Post3=NewPost ("C"); Post1.add (NewDisc ("3", 2)); Post1.add (NewDisc ("2", 1)); Post1.add (NewDisc ("1", 0));        System.out.println (POST1);        System.out.println (POST2);                System.out.println (POST3); Post1.move (Post2, Post3,NewPostmove () {@Override Public voidaction (Post srcpost, post Middlepost,post targetpost, Disc Disc) {System.out.println (Disc+ ": from" + srcpost.getname () + "through" + middlepost.getname () + "arrival" +targetpost.getname ());        }        });        System.out.println (POST1);        System.out.println (POST2);    System.out.println (POST3); }}

The question of the Nottingham Tower implemented by the idea of Java object-oriented

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.