external and internal invocation of methods such as Java Learning

Source: Internet
Author: User

  1. External calls

    An external call is a method that calls another class in one class

    Package hello2;//first defines a class

    public class Book {
    String name;
    String author;
    Double Price;

    public void GetInfo () {
    Output title, author, and Price as a string
    System.out.println ("The title is" +name+ ", the author is" +author+ ", The Price is" +price ");
    }
    }
    Second, apply the first class method in another class

    Package Hello2;
    public class Testbook {
    public static void Main (string[] args)
    {
    Book B1=new book ();
    Book B2=new book ();//Use an external call to create an object first
    The operator that uses the object.
    B1.name= "Java";
    B1.author= "Zhangsan";
    b1.price=10.45;
    B1.getinfo ();////method Internal call, directly call if the class's properties and methods in another class, first create an object, B1.getinfo ();
    B2.name= "ADSFAF";
    B2.author= "K;l";
    b2.price=100;
    B2.getinfo ();
    }

    }

  2. Internal call

    Internal calls are called within a class, and can be called directly from the method name.

    Package Hello2;
    public class Testbook {
    static String name;
    static String author;
    static double price;

    public static void GetInfo () {
    Output title, author, and Price as a string
    System.out.println ("The title is" +name+ ", the author is" +author+ ", The Price is" +price ");
    }
    public static void Main (string[] args)//static method calls another method, another method must also be a static method, so GetInfo () is a static method.
    {
    Testbook b1=new Testbook ();
    Testbook b2=new Testbook ();//Create Object
    The operator that uses the object.
    B1.name= "Java";
    B1.author= "Zhangsan";
    b1.price=10.45;
    GetInfo ();
    }}

external and internal invocation of methods such as Java Learning

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.