0
public class StudentA extends marks \\child class
{
    public double maths;
    public double chemistry;
    public double physics;
    double percentage;

    public StudentA(integer maths, integer chemistry, integer physics)
    {
        system.debug('maths marks are' + maths);
        system.debug('chemistry marks are' + chemistry);
        system.debug('physics marks are' + physics);
    }
    
    public double percentage()
    {

        percentage = (maths+chemistry+physics/3);
        
        system.debug('percentage is' + percentage);
        
        return percentage;
    }
      
    public override void getpercentage()
    { 
        system.debug('total percent' + percentage);
           
    }
   
    
}


object: 

StudentA v = new StudentA(99,98,99);
v.percentage();
v.getpercentage();

注意:我正在尝试计算获得的标记的总百分比,当我执行它时给我错误“尝试取消引用空对象”。

在输出中只打印标记。在此处输入图像描述 错误屏幕截图并执行输出。

在此处输入图像描述mgur.com/tiJe2.png

4

2 回答 2

1

您没有将参数值分配给类变量

请修改您的代码如下:

public class StudentA extends marks \\child class
    {
    public double maths;
    public double chemistry;
    public double physics;
    double percentage; // can remove this line

    public StudentA(integer maths, integer chemistry, integer physics)
    {
        this.maths = maths;
        this.chemistry = chemistry;
        this.physics = maths;
    }
    
    public double percentage()
    {

        percentage = (maths+chemistry+physics/3);
        
        system.debug('percentage is' + percentage);
        
        return percentage;
    }
      
    public override void getpercentage()
    { 
        system.debug('total percent' + percentage);
           
    }
  }

     StudentA v = new StudentA(99,98,99);
     v.percentage();
     v.getpercentage();
于 2021-09-01T10:07:08.383 回答
0

您正在传递变量但没有设置它们。

在你的构造函数中做

Maths = maths;
Chemistry = Chemistry;
Physics = Physics;
于 2021-08-04T13:04:56.013 回答