全量示例

class

apex 中允许定义一层子类(不允许定义孙子类)


public class myOuterClass {

   class myInnerClass {

   }
}

外层类必须使用的修饰符 public private global , 子类可以不使用修饰符

  • private 只能在子类或者外层测试类中使用(@isTest),子类不使用修饰符,默认为 private
  • public 该类在您的应用程序或名称空间中可见
  • global 全局可见,REST等webservice类必须声明为全局,如果子类或内部方法声明为全局,则外部类也必须声明为全局

可选的修饰符还有 virtual abstract with sharing without sharing

  • virtual 声明该类允许扩展和覆盖,使用 override 覆盖类方法
  • abstract 抽象类
  • with sharing without sharing

可以继承和实现接口(单继承多实现)

不要将标准对象名称作为类名称

private | public | global 
[virtual | abstract | with sharing | without sharing] 
class ClassName [implements InterfaceNameList] [extends ClassName] 
{ 

}

成员变量


[public | private | protected | global] [final] [static] data_type variable_name [= value]

方法

可以定义多态方法

override 须配合 virtual 使用

基础类型参数传值,对象类型参数传引用,方法内不允许修改引用,但可以修改值


[public | private | protected | global] [override] [static] data_type method_name 
(input parameters) 
{
// The body of the method
}

构造函数

针对不同参数可以写多个构造函数


public class TestObject {

    public TestObject() {

    }

    public TestObject(Integer i) {

    }

}

静态字段、静态方法

静态字段、方法只能在外层类中使用

实例化类之前,已经初始化类静态字段、方法,所以静态方法不能使用成员变量

静态变量在 APEX 事务范围内是静态的

跨实例间可以用静态变量共享数据,比如:防止一个触发器在一次事务中重复触发等

实例初始化代码

每次实例化对象时,都会执行实例初始化代码,它先用构造函数执行


public class MyClass {

    class RGB {

        Integer red;
        Integer green;
        Integer blue;

        RGB(Integer red, Integer green, Integer blue) {
            this.red = red;
            this.green = green;
            this.blue = blue;
        }
     }

   static Map<String, RGB> colorMap = new Map<String, RGB>();

    static {
        colorMap.put('red', new RGB(255, 0, 0));
        colorMap.put('cyan', new RGB(0, 255, 255));
        colorMap.put('magenta', new RGB(255, 0, 255));
    }
}

get、set

可以使用 get、set 在设置和获取字段值得时候做一些逻辑处理


public class BasicProperty {
   public integer prop {
      get { return prop; }
      set { prop = value; }
   }
}

可以设置字段只读、只写等


public class AutomaticProperty {
   public integer MyReadOnlyProp { get; }            // 只读
   public double MyReadWriteProp { get; set; }       // 读写
   public string MyWriteOnlyProp { set; }            // 只写
}

静态属性


public class StaticProperty {
   private static integer StaticMember;
   private integer NonStaticMember;

   // The following produces a system error
   // public static integer MyBadStaticProp { return NonStaticMember; }

   public static integer MyGoodStaticProp { 
     get {return StaticMember;} 
     set { StaticMember = value; } 
   }  
   public integer MyGoodNonStaticProp { 
     get {return NonStaticMember;} 
     set { NonStaticMember = value; } 
   } 
}

属性访问器修饰符

  • 优先级高于字段修饰符
  • 必须比字段修饰符更具限制性

global virtual class PropertyVisibility {
   // X is private for read and public for write
   public integer X { private get; set; }
   // Y can be globally read but only written within a class
   global integer Y { get; public set; }
   // Z can be read within the class but only subclasses can set it
   public integer Z { get; protected set; }
}

继承

public virtual class Marker {
    public virtual void write() {
        System.debug('hello world father');
    }

    public virtual Double discount() {
        return .05;
    }
}

// 继承父类
public class YellowMarker extends Marker {
    // 重写父类的方法
    public override void write() {
        System.debug('hello world son');
    } 
}

接口


// An interface that defines what a purchase order looks like in general
public interface PurchaseOrder {
    // All other functionality excluded
    Double discount();
}


// One implementation of the interface for customers
public class CustomerPurchaseOrder implements PurchaseOrder {
    public Double discount() {
        return .05;  // Flat 5% discount
    }
}

// Another implementation of the interface for employees
public class EmployeePurchaseOrder implements PurchaseOrder {
      public Double discount() {
        return .10;  // It’s worth it being an employee! 10% discount
      } 
}

迭代器接口

迭代器

results matching ""

    No results matching ""