A review of the Java object-oriented approach

Source: Internet
Author: User
Tags class definition

1.1 Review of methods
1.1.1 Case Code One:

package com.itheima_01;/* * 需求:定义一个方法求两个数的和,并在主方法中调用 * * 方法:类中的一段具有特定功能的程序,提高了代码的复用性和可维护性 * 定义格式: * public static 返回值类型(没有返回值写void) 方法名(参数类型 参数名,参数类型 参数名2) {//形参 * 方法体; * } * 调用方式: * 有明确返回值类型: * 赋值调用,将方法的返回值赋值给一个变量 * 输出调用,使用输出语句直接输出方法的返回值 * 直接调用,没法获取方法的返回值 * 没有明确返回值类型: * 直接调用 * 方法重载:在一个类中有多个重名的方法,这些方法参数不同,和返回值无关 * * 注意: * 形参:方法声明的变量,只能是变量,接收方法调用时传递进来的数据 * 实参:调用方法时传递的数据,可以是常量也可以是变量 * */public class MethoDemo {public static void main(String[] args) {//赋值调用//int sum = sum(10,20);//实参//System.out.println(sum);//输出调用int a = 10;int b = 20;System.out.println(sum(a,b));}public static int sum(int a,int b) {/*        //使用变量接收求和结果并返回int sum = a + b;return sum;*///直接返回求和结果return a + b;}}

1.2 Review of arrays
1.2.1 Case code Two:

package com.itheima_02;/* * 需求:定义一个元素类型为int的数组,遍历数组并求和 * * 数组:用于存储多个元素的一种容器 * 数组的特点: * 元素类型必须一致 * 元素有整数索引 * 一旦定义好长度则无法改变 * 可以存储基本数据类型 * 也可以存储引用数据类型 * 定义格式: * 动态初始化 * 元素类型[] 数组名 = new 元素类型[10]; * 静态初始化 * 元素类型[] 数组名 = {元素1,元素2,元素3}; * 元素类型[] 数组名  = new 元素类型[]{元素1,元素2,元素3}; * */public class ArrayDemo {public static void main(String[] args) {//使用静态初始化定义数组int[] arr = {1,2,3,4,5};//定义一个变量用于存储求和结果int sum = 0;//遍历数组for(int x = 0;x < arr.length;x++) {sum += arr[x];}System.out.println(sum);}}

1.3 Standard class definition and usage review
1.3.1 Case Code Three:

Package com.itheima_03;/* * Defines a standard student class, creates an object in the main method and invokes * name, age, gender 3 member variable * No parameter, two constructor method * Defines a show party for each member variable defined by the Getter/setter method * method, output member variable */public class Student {private string name;//name private int age;//Age private String gender;//gender/*//non-parametric construction public Stu Dent () {}//has an argument construct public Student (String name,int age,string gender) {this.name = Name;this.age = Age;this.gender = gender;} Namepublic String GetName () {return name;} public void SetName (String name) {this.name = name;} agepublic int Getage () {return age;} public void Setage (int.) {this.age = age;} Genderpublic String Getgender () {return gender;} public void Setgender (String gender) {This.gender = gender;} *///show: Used to output all member variables public void Show () {System.out.println (name + "," + Age + "," + Gender);} Public Student () {super ();//TODO auto-generated constructor stub}public Student (string name, int age, string gender) {SUP ER (); this.name = Name;this.age = Age;this.gender = gender;} Public String GetName () {return name;} public void SetName (String name) {thiS.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String Getgender () {return gender;} public void Setgender (String gender) {This.gender = gender;}} Package Com.itheima_03;public class Studenttest {public static void main (string[] args) {//Create student object Student s = new Student () ;//Assign value to member variable S.setname ("Zhang San"); S.setage (); S.setgender ("male"); S.show (); System.out.println ("----------"); Student s2 = new Student ("John Doe", 20, "other");//s2.show (); System.out.println (S2.getname ());}}

Review of Java object-oriented methods

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.