通过派生类型调用的静态方法

时间:2015-05-18 08:09:54

标签: c# sharpdevelop

有一个叫做圆圈的课。它包含circleID,circleGeometry和circlePath属性。

public class Circle 
{
    public string ID {get; set;}
    public EllipseGeometry circleGeometry {get; set;}
    public Path circlePath {get; set;}
}

现在,我尝试为圆圈设置ZIndex值。例如,它将是2。

Canvas.SetZIndex(someCircleID.circlePath,2);

但我有这样的警告:

  

"通过派生类型"

调用静态方法

有人可以解释一下这是什么意思吗?

2 个答案:

答案 0 :(得分:8)

SetZIndexPanel类上定义,Canvas派生自SetZIndex。编译器正在生成警告,说明您在子类型上使用静态方法。这不是一个真正的问题,但在某些方面使用时可能会导致混淆。由于var ftpRequest = (FtpWebRequest) HttpWebRequest.Create("ftp://my.ftp.com"); 无效返回,这不应该是一个问题。

但想象一下:

Create

WebRequest实际上是HttpWebRequest的静态方法,但在FtpWebRequest上使用,因为它是派生类型,而可能这样做。所以,您希望它是一个正在生成的Web请求,对吗?但它不是,它会生成SetZIndex,因为它在URI中指定。

修改

我想指出的是,通常情况下,编译器警告是有原因的,也就是存在这种情况。只要在Canvas类中没有创建Canvas的重载,呼叫就是安全的。但正如@SriramSakthivel在评论中指出的那样,如果在此过程中,您或其他任何人在new类(使用 import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import android.app.Activity; import android.app.AlarmManager; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CalendarView; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TextView; import android.widget.Toast; public class DatePickerExample extends Activity { private TextView Output; private Button changeDate; private Boolean mEnableNativeCalGridView = null; String timeZone; public static TimeZone tz; private int year; private int month; DatePickerDialog d; public Calendar c; SimpleDateFormat sdf; private int day; public static IntentFilter s_intentFilter; static final int DATE_PICKER_ID = 1111; static{ s_intentFilter = new IntentFilter(); s_intentFilter.addAction(Intent.ACTION_TIME_TICK); s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); registerReceiver(m_timeChangedReceiver, s_intentFilter); Output = (TextView) findViewById(R.id.Output); changeDate = (Button) findViewById(R.id.changeDate); // sdf = new SimpleDateFormat("EEE, MMM d, ''yy");//Wed, Jul 4, '01 // Show current date Output.setText(new StringBuilder() // Month is 0 based, just add 1 .append(month + 1).append("-").append(day).append("-") .append(year).append(" ")); // Button listener to show date picker dialog changeDate.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { // On button click show datepicker dialog showDialog(DATE_PICKER_ID); } }); } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DATE_PICKER_ID: ((DatePickerDialog) dialog).updateDate( c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener listner to date picker sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); tz = TimeZone.getDefault(); System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID()); timeZone = tz.getDisplayName(false, TimeZone.SHORT); c= Calendar.getInstance(); c.setTimeZone(tz); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); d = new DatePickerDialog(DatePickerExample.this, pickerListener, year, month,day); setMinMaxdate(); return d; } return null; } ////////////////////Fix for issue on OS 5.1 ////////////////////// public long getDateFormatted(String date){ String givenDateString = date; long timeInMilliseconds = 0; try { Date mDate = sdf.parse(givenDateString); timeInMilliseconds = mDate.getTime(); System.out.println("Date in milli :: " + timeInMilliseconds); } catch (ParseException e) { e.printStackTrace(); } return timeInMilliseconds; } ////////////////////////////////////////// private DatePicker.OnDateChangedListener newchange= new OnDateChangedListener(){ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) { Toast.makeText(getApplicationContext(), "onDateChanged", Toast.LENGTH_SHORT).show(); }}; DatePicker datePicker; public void setMinMaxdate() { //Time zone calculation //TimeZone.setDefault(tz); /////////////////////// long calEvtEndDate= getDateFormatted("Sun May 31 23:59:59 "+timeZone+" 2015");//System.currentTimeMillis(); long calEvtStartDate= getDateFormatted("Wed May 13 00:00:00 "+timeZone+" 2015");//System.currentTimeMillis()/2; // long calEvtEndDate = getDateFormatted("Sun, may 31, '15"); //long calEvtStartDate = getDateFormatted("Wed, may 13, '15"); if(d != null){ datePicker = d.getDatePicker(); if(mEnableNativeCalGridView != null){ datePicker.setCalendarViewShown(mEnableNativeCalGridView.booleanValue()); } // If Start Date is Greater than End Date then we are showing from valid StartDate // value and we are not setting the maxdate. if (calEvtStartDate > calEvtEndDate) { datePicker.setMinDate(calEvtStartDate); } else { if (calEvtStartDate > 0) { // If Only ValidStart date is provided, then setting the minDate. datePicker.setMinDate(calEvtStartDate); } if (calEvtEndDate > 0) { // If Only ValidEnd date is provided, then setting the maxDate. datePicker.setMaxDate(calEvtEndDate); } } } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("-------resumed"); } public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { //view.updateDate(year, month, day); System.out.println("---------------datesetchange"); year = selectedYear; month = selectedMonth; day = selectedDay; // Show selected date Output.setText(new StringBuilder().append(month + 1) .append("-").append(day).append("-").append(year) .append(" ")); } }; public void onDestroy() { super.onDestroy(); unregisterReceiver(m_timeChangedReceiver); } private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { System.out.println("timezone changed---"+action.toString()); tz = TimeZone.getDefault(); System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID()); timeZone = tz.getDisplayName(false, TimeZone.SHORT); // Intent intent1 = getIntent(); // finish(); // startActivity(intent1); showDialog(DATE_PICKER_ID); } } }; }修饰符)中添加了任何扩展方法或静态方法,它将会不再输出所需的结果,你必须意识到这一点。

答案 1 :(得分:2)

这意味着方法SetZIndex是在类Canvas的基类型上定义的,但您可以使用后者来调用它。你最好使用基类。