1.普通的class類呼叫
*類定義
public section.
methods:show_text."定義的方法名 show_text
private section.
data text(100) type c value 'this is my first abap object.'.
endclass.
*類實現
write text.
endmethod.
endclass.
*第二個例子
*類定義
class vehicle definition.
public section.
class-data class_name(10) type c value 'vehicle'.
methods:accelerate,"定義的方法 accelerate 和 show_speed
show_speed.
protected section.
data speed type i.
endclass.
class vehicle implementation."方法的實現
method accelerate.
speed = speed + 1.
endmethod.
method show_speed.
write: / 'speed:',speed.
endmethod.
endclass.
start-of-selection.
*呼叫類中的方法
*類呼叫
data obj_vehicle type ref to vehicle.
create object obj_vehicle.
do 10 times.
call method obj_vehicle->accelerate.
enddo.
call method obj_vehicle->show_speed.
2.構造輸入輸出引數
class circle definition.
public section.
methods get_area importing value(i_radius) type i
returning value(r_size) type f.
private section.
* 定義靜態變數
constants pi type f value '3.14159265'.
endclass.
class circle implementation.
method get_area.
r_size = i_radius ** 2 * pi.
endmethod.
endclass.
*彈出引數輸入螢幕
parameters radius type i.
data: o_circle type ref to circle,
area type f.
start-of-selection.
create object o_circle.
call method o_circle->get_area
exporting i_radius = radius
receiving r_size = area.
write: / area.
3.傳遞構造引數
class vehicle definition.
public section.
methods: accelerate importing rate type i,
* 定義建構函式,有乙個輸入引數
constructor importing i_speed type i,
show_speed.
private section.
data speed type i value 0.
endclass.
class vehicle implementation.
method accelerate.
speed = speed + rate.
endmethod.
method show_speed.
write: / speed.
endmethod.
method constructor.
speed = i_speed.
endmethod.
endclass.
data o_vehicle type ref to vehicle.
start-of-selection.
* 這個建構函式傳了引數進去
create object o_vehicle exporting i_speed = 4.
call method o_vehicle->accelerate exporting rate = 2.
call method o_vehicle->accelerate exporting rate = 2.
call method o_vehicle->show_speed.
C 複習 類(class)的實現與定義
一 基本概念 類就是對現實世界中的具有相同屬性物件的抽象描述 一般包括屬性和方法 分為基類 子類 和派生類。二 類的定義 class 類名 需要注意的點 1 類名需要遵循一定的命名規則 2 public和private是屬性和行為的關鍵字 一般類的屬性成員應設定為private public只留給那...
Java反射 獲取Class及Class對應資訊
1.獲取構造器 constructor getconstructor class.parametertypes 返回 public 指定形參的 constructor getconstructors 返回 public 所有的constructor getdeclaredconstructor cl...
類 Class 物件 定義 方法
類 class 用來描述具體相同的屬性和方法的物件的集合。定義了該集合中每個物件所共有的屬性和方法。物件是類的示例。類變數 類變數在整個例項化的物件中是公用的。類變數定義在類中且在函式體之外。類變數通常不作為例項變數使用。方法重寫 如果從父類繼承的方法不能滿足子類的需求,可以對其 進行改寫,這個過程...