Basic Application of java reference Transfer

Source: Internet
Author: User
Tags fun call java reference

Basic Application of java reference Transfer
Package com. cloud. day1;


Public class Demo1 {
Public static void main (String [] args ){
// Basic application of reference Transfer
Demo2 d2 = new Demo2 ();
D2.temp = 500;
System. out. println (before fun call: + d2.temp );
Fun (d2 );
System. out. println (+ d2.temp after fun is called );
System. out. println (**********);
String str1 = hello;
System. out. println (before calling fun1: + str1 );
Fun1 (str1 );
System. out. println (after calling fun1: + str1 );
System. out. println (**********);
Demo3 d3 = new Demo3 ();
D3.temp = world;
System. out. println (before calling fun1: + d3.temp );
Fun2 (d3 );
System. out. println (after fun1 is called: + d3.temp );
// Receives the reference of your own class
System. out. println (**********);
Demo4 d4 = new Demo4 ();
D4.setTemp (100 );
D4.fun (d4 );
System. out. println (d4.getTemp ());
// Case Study: one-to-one correspondence
Person per = new Person (Tom, 20 );
Book bk = new Book (JAVA tutorial, 50366f );
Per. setBook (bk );
Bk. setPerson (per );
System. out. println (for books: + per. getName () + -- + per. getBook (). getTitle () + -- + per. getBook (). getPrice ());
System. out. println (Book: + bk. getTitle () + -- + bk. getPerson (). getName ());
}
Public static void fun (Demo2 d2 ){
D2.temp = 1000;
}
Public static void fun1 (String str2 ){
Str2 = mdld;
}
Public static void fun2 (Demo3 d3 ){
D3.temp = mldl;
}
}
Class Demo2 {
Int temp = 100;
}
Class Demo3 {
String temp = hello;
}
Class Demo4 {
Private int temp = 30;
Public void fun (Demo4 d4 ){
D4.temp = 40;
}
Public int getTemp (){
Return temp;
}
Public void setTemp (int temp ){
This. temp = temp;
}
}
Class Person {
Private String name;
Private int age;
Private Book;
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
Public Book getBook (){
Return book;
}
Public void setBook (Book book ){
This. book = book;
}
Public Person (String name, int age ){
This. setName (name );
This. setAge (age );
}
}
Class Book {
Private String title;
Private float price;
Private Person person;
Public Book (String title, float price ){
This. setTitle (title );
This. setPrice (price );
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
Public float getPrice (){
Return price;
}
Public void setPrice (float price ){
This. price = price;
}
Public Person getPerson (){
Return person;
}
Public void setPerson (Person person ){
This. person = person;
}

}

The last case is very important. It is applied to the three major Java Web frameworks.

 

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.