A small exercise in Java objects and classes, Java object exercises

Source: Internet
Author: User

A small exercise in Java objects and classes, Java object exercises

I have been doing exercises in Eclipse. Is to do an exercise and execute one. I just learned about Java objects and classes. In practice, I put classes and execution in two. java files under the same package. Yes. (Get)

Related code:

1 public class Calc {2 // Its Attribute 3 int width = 90; 4 int height = 180; 5 String color = "green "; 6 7 // Method 8 int jia (int a, int B) {9 return a + B; 10} 11 12 int jian (int a, int B) {13 return a-B; 14} 15 16 void jieshao () {17 System. out. println ("I Am a calculator"); 18 System. out. println ("My length is:" + height); 19 System. out. println ("My width is:" + width); 20 System. out. println ("My color:" + color); 21} 22}
1 public class TestCalc {2 public static void main (String [] args) {3 Calc c = new Calc (); 4 5 c. jieshao (); 6 7 int a = 10; 8 int B = 20; 9 10 int result = c. jia (a, B); 11 12 int result2 = c. jian (a, B); 13 14 System. out. println ("addition result:" + result); 15 System. out. println ("subtraction Result:" + result2); 16} 17}

Note: During the call, the main function must re-assign values to the class (in the above example, Calc c =NewCalc ();)

I understand the rest.

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.